Skip to content

Commit 9682a93

Browse files
committed
Add test for errors on wrongly requested methods
1 parent 698e905 commit 9682a93

File tree

1 file changed

+59
-20
lines changed

1 file changed

+59
-20
lines changed

test/test_typed.jl

+59-20
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
1-
@testset "Message dispatcher" begin
2-
1+
function socket_path(id)
32
if Sys.iswindows()
4-
global_socket_name1 = "\\\\.\\pipe\\jsonrpc-testrun1"
3+
return "\\\\.\\pipe\\jsonrpc-testrun$id"
54
elseif Sys.isunix()
6-
global_socket_name1 = joinpath(tempdir(), "jsonrpc-testrun1")
5+
return joinpath(mktempdir(), "jsonrpc-testrun$id")
76
else
87
error("Unknown operating system.")
98
end
9+
end
10+
11+
@testset "Message dispatcher" begin
12+
13+
global_socket_name1 = socket_path(1)
1014

1115
request1_type = JSONRPC.RequestType("request1", Foo, String)
1216
request2_type = JSONRPC.RequestType("request2", Nothing, String)
1317
notify1_type = JSONRPC.NotificationType("notify1", String)
1418

1519
global g_var = ""
1620

17-
server_is_up = Base.Condition()
21+
server_is_up1 = Base.Condition()
1822

1923
server_task = @async try
2024
server = listen(global_socket_name1)
21-
notify(server_is_up)
25+
notify(server_is_up1)
26+
yield() # don't want to deadlock
2227
sock = accept(server)
2328
global conn = JSONRPC.JSONRPCEndpoint(sock, sock)
24-
global msg_dispatcher = JSONRPC.MsgDispatcher()
29+
msg_dispatcher = JSONRPC.MsgDispatcher()
2530

2631
msg_dispatcher[request1_type] = (conn, params) -> begin
2732
@test JSONRPC.is_currently_handling_msg(msg_dispatcher)
@@ -39,7 +44,7 @@
3944
Base.display_error(stderr, err, catch_backtrace())
4045
end
4146

42-
wait(server_is_up)
47+
wait(server_is_up1)
4348

4449
sock2 = connect(global_socket_name1)
4550
conn2 = JSONRPCEndpoint(sock2, sock2)
@@ -63,22 +68,17 @@
6368

6469
# Now we test a faulty server
6570

66-
if Sys.iswindows()
67-
global_socket_name2 = "\\\\.\\pipe\\jsonrpc-testrun2"
68-
elseif Sys.isunix()
69-
global_socket_name2 = joinpath(tempdir(), "jsonrpc-testrun2")
70-
else
71-
error("Unknown operating system.")
72-
end
71+
global_socket_name2 = socket_path(2)
7372

74-
server_is_up = Base.Condition()
73+
server_is_up2 = Base.Condition()
7574

7675
server_task2 = @async try
7776
server = listen(global_socket_name2)
78-
notify(server_is_up)
77+
notify(server_is_up2)
78+
yield() # don't want to deadlock
7979
sock = accept(server)
8080
global conn = JSONRPC.JSONRPCEndpoint(sock, sock)
81-
global msg_dispatcher = JSONRPC.MsgDispatcher()
81+
msg_dispatcher = JSONRPC.MsgDispatcher()
8282

8383
msg_dispatcher[request2_type] = (conn, params)->34 # The request type requires a `String` return, so this tests whether we get an error.
8484

@@ -91,7 +91,7 @@
9191
Base.display_error(stderr, err, catch_backtrace())
9292
end
9393

94-
wait(server_is_up)
94+
wait(server_is_up2)
9595

9696
sock2 = connect(global_socket_name2)
9797
conn2 = JSONRPCEndpoint(sock2, sock2)
@@ -104,6 +104,45 @@
104104
close(sock2)
105105
close(conn)
106106

107-
fetch(server_task)
107+
fetch(server_task2)
108+
109+
110+
# Now we test a wrongly requested method
111+
112+
global_socket_name3 = socket_path(3)
113+
114+
server_is_up3 = Base.Condition()
115+
116+
server_task3 = @async try
117+
server = listen(global_socket_name3)
118+
notify(server_is_up3)
119+
yield() # don't want to deadlock
120+
sock = accept(server)
121+
global conn = JSONRPC.JSONRPCEndpoint(sock, sock)
122+
msg_dispatcher = JSONRPC.MsgDispatcher()
123+
124+
run(conn)
125+
126+
for msg in conn
127+
@test_throws ErrorException("Unknown method 'request2'.") JSONRPC.dispatch_msg(conn, msg_dispatcher, msg)
128+
flush(conn)
129+
end
130+
catch err
131+
Base.display_error(stderr, err, catch_backtrace())
132+
end
133+
134+
wait(server_is_up3)
135+
136+
sock3 = connect(global_socket_name3)
137+
conn3 = JSONRPCEndpoint(sock3, sock3)
138+
139+
run(conn3)
140+
141+
@test_throws JSONRPC.JSONRPCError(-32601, "Unknown method 'request2'.", nothing) JSONRPC.send(conn3, request2_type, nothing)
142+
143+
close(conn3)
144+
close(sock3)
145+
close(conn)
146+
fetch(server_task3)
108147

109148
end

0 commit comments

Comments
 (0)