|
1 |
| -@testset "Message dispatcher" begin |
2 |
| - |
| 1 | +function socket_path(id) |
3 | 2 | if Sys.iswindows()
|
4 |
| - global_socket_name1 = "\\\\.\\pipe\\jsonrpc-testrun1" |
| 3 | + return "\\\\.\\pipe\\jsonrpc-testrun$id" |
5 | 4 | elseif Sys.isunix()
|
6 |
| - global_socket_name1 = joinpath(tempdir(), "jsonrpc-testrun1") |
| 5 | + return joinpath(mktempdir(), "jsonrpc-testrun$id") |
7 | 6 | else
|
8 | 7 | error("Unknown operating system.")
|
9 | 8 | end
|
| 9 | +end |
| 10 | + |
| 11 | +@testset "Message dispatcher" begin |
| 12 | + |
| 13 | + global_socket_name1 = socket_path(1) |
10 | 14 |
|
11 | 15 | request1_type = JSONRPC.RequestType("request1", Foo, String)
|
12 | 16 | request2_type = JSONRPC.RequestType("request2", Nothing, String)
|
13 | 17 | notify1_type = JSONRPC.NotificationType("notify1", String)
|
14 | 18 |
|
15 | 19 | global g_var = ""
|
16 | 20 |
|
17 |
| - server_is_up = Base.Condition() |
| 21 | + server_is_up1 = Base.Condition() |
18 | 22 |
|
19 | 23 | server_task = @async try
|
20 | 24 | server = listen(global_socket_name1)
|
21 |
| - notify(server_is_up) |
| 25 | + notify(server_is_up1) |
| 26 | + yield() # don't want to deadlock |
22 | 27 | sock = accept(server)
|
23 | 28 | global conn = JSONRPC.JSONRPCEndpoint(sock, sock)
|
24 |
| - global msg_dispatcher = JSONRPC.MsgDispatcher() |
| 29 | + msg_dispatcher = JSONRPC.MsgDispatcher() |
25 | 30 |
|
26 | 31 | msg_dispatcher[request1_type] = (conn, params) -> begin
|
27 | 32 | @test JSONRPC.is_currently_handling_msg(msg_dispatcher)
|
|
39 | 44 | Base.display_error(stderr, err, catch_backtrace())
|
40 | 45 | end
|
41 | 46 |
|
42 |
| - wait(server_is_up) |
| 47 | + wait(server_is_up1) |
43 | 48 |
|
44 | 49 | sock2 = connect(global_socket_name1)
|
45 | 50 | conn2 = JSONRPCEndpoint(sock2, sock2)
|
|
63 | 68 |
|
64 | 69 | # Now we test a faulty server
|
65 | 70 |
|
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) |
73 | 72 |
|
74 |
| - server_is_up = Base.Condition() |
| 73 | + server_is_up2 = Base.Condition() |
75 | 74 |
|
76 | 75 | server_task2 = @async try
|
77 | 76 | server = listen(global_socket_name2)
|
78 |
| - notify(server_is_up) |
| 77 | + notify(server_is_up2) |
| 78 | + yield() # don't want to deadlock |
79 | 79 | sock = accept(server)
|
80 | 80 | global conn = JSONRPC.JSONRPCEndpoint(sock, sock)
|
81 |
| - global msg_dispatcher = JSONRPC.MsgDispatcher() |
| 81 | + msg_dispatcher = JSONRPC.MsgDispatcher() |
82 | 82 |
|
83 | 83 | msg_dispatcher[request2_type] = (conn, params)->34 # The request type requires a `String` return, so this tests whether we get an error.
|
84 | 84 |
|
|
91 | 91 | Base.display_error(stderr, err, catch_backtrace())
|
92 | 92 | end
|
93 | 93 |
|
94 |
| - wait(server_is_up) |
| 94 | + wait(server_is_up2) |
95 | 95 |
|
96 | 96 | sock2 = connect(global_socket_name2)
|
97 | 97 | conn2 = JSONRPCEndpoint(sock2, sock2)
|
|
104 | 104 | close(sock2)
|
105 | 105 | close(conn)
|
106 | 106 |
|
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) |
108 | 147 |
|
109 | 148 | end
|
0 commit comments