|
1 |
| -@testitem "Message dispatcher" begin |
| 1 | +@testitem "Dynamic message dispatcher" setup=[TestStructs] begin |
2 | 2 | using Sockets
|
| 3 | + using .TestStructs: Foo, Foo2 |
3 | 4 |
|
4 |
| - include("shared_test_code.jl") |
5 |
| - |
6 |
| - if Sys.iswindows() |
7 |
| - global_socket_name1 = "\\\\.\\pipe\\jsonrpc-testrun1" |
8 |
| - elseif Sys.isunix() |
9 |
| - global_socket_name1 = joinpath(tempdir(), "jsonrpc-testrun1") |
10 |
| - else |
11 |
| - error("Unknown operating system.") |
12 |
| - end |
| 5 | + global_socket_name1 = JSONRPC.generate_pipe_name() |
13 | 6 |
|
14 | 7 | request1_type = JSONRPC.RequestType("request1", Foo, String)
|
15 | 8 | request2_type = JSONRPC.RequestType("request2", Nothing, String)
|
|
66 | 59 |
|
67 | 60 | # Now we test a faulty server
|
68 | 61 |
|
69 |
| - if Sys.iswindows() |
70 |
| - global_socket_name2 = "\\\\.\\pipe\\jsonrpc-testrun2" |
71 |
| - elseif Sys.isunix() |
72 |
| - global_socket_name2 = joinpath(tempdir(), "jsonrpc-testrun2") |
73 |
| - else |
74 |
| - error("Unknown operating system.") |
75 |
| - end |
| 62 | + global_socket_name2 = JSONRPC.generate_pipe_name() |
76 | 63 |
|
77 | 64 | server_is_up = Base.Condition()
|
78 | 65 |
|
@@ -121,3 +108,106 @@ end
|
121 | 108 | @test typed_res(['f','o','o'], String) isa String
|
122 | 109 | @test typed_res("foo", String) isa String
|
123 | 110 | end
|
| 111 | + |
| 112 | +@testitem "Static message dispatcher" setup=[TestStructs] begin |
| 113 | + using Sockets |
| 114 | + using .TestStructs: Foo, Foo2 |
| 115 | + |
| 116 | + global_socket_name1 = JSONRPC.generate_pipe_name() |
| 117 | + |
| 118 | + request1_type = JSONRPC.RequestType("request1", Foo, String) |
| 119 | + request2_type = JSONRPC.RequestType("request2", Nothing, String) |
| 120 | + notify1_type = JSONRPC.NotificationType("notify1", String) |
| 121 | + |
| 122 | + global g_var = "" |
| 123 | + |
| 124 | + server_is_up = Base.Condition() |
| 125 | + |
| 126 | + JSONRPC.@message_dispatcher my_dispatcher begin |
| 127 | + request1_type => (conn, params) -> begin |
| 128 | + params.fieldA == 1 ? "YES" : "NO" |
| 129 | + end |
| 130 | + request2_type => (conn, params) -> JSONRPC.JSONRPCError(-32600, "Our message", nothing) |
| 131 | + notify1_type => (conn, params) -> global g_var = params |
| 132 | + end |
| 133 | + |
| 134 | + server_task = @async try |
| 135 | + server = listen(global_socket_name1) |
| 136 | + notify(server_is_up) |
| 137 | + sock = accept(server) |
| 138 | + global conn = JSONRPC.JSONRPCEndpoint(sock, sock) |
| 139 | + global msg_dispatcher = JSONRPC.MsgDispatcher() |
| 140 | + |
| 141 | + run(conn) |
| 142 | + |
| 143 | + for msg in conn |
| 144 | + my_dispatcher(conn, msg) |
| 145 | + end |
| 146 | + catch err |
| 147 | + Base.display_error(stderr, err, catch_backtrace()) |
| 148 | + end |
| 149 | + |
| 150 | + wait(server_is_up) |
| 151 | + |
| 152 | + sock2 = connect(global_socket_name1) |
| 153 | + conn2 = JSONRPCEndpoint(sock2, sock2) |
| 154 | + |
| 155 | + run(conn2) |
| 156 | + |
| 157 | + JSONRPC.send(conn2, notify1_type, "TEST") |
| 158 | + |
| 159 | + res = JSONRPC.send(conn2, request1_type, Foo(fieldA=1, fieldB="FOO")) |
| 160 | + |
| 161 | + @test res == "YES" |
| 162 | + @test g_var == "TEST" |
| 163 | + |
| 164 | + @test_throws JSONRPC.JSONRPCError(-32600, "Our message", nothing) JSONRPC.send(conn2, request2_type, nothing) |
| 165 | + |
| 166 | + close(conn2) |
| 167 | + close(sock2) |
| 168 | + close(conn) |
| 169 | + |
| 170 | + fetch(server_task) |
| 171 | + |
| 172 | + # Now we test a faulty server |
| 173 | + |
| 174 | + global_socket_name2 = JSONRPC.generate_pipe_name() |
| 175 | + |
| 176 | + server_is_up = Base.Condition() |
| 177 | + |
| 178 | + JSONRPC.@message_dispatcher my_dispatcher2 begin |
| 179 | + request2_type => (conn, params) -> 34 # The request type requires a `String` return, so this tests whether we get an error. |
| 180 | + end |
| 181 | + |
| 182 | + server_task2 = @async try |
| 183 | + server = listen(global_socket_name2) |
| 184 | + notify(server_is_up) |
| 185 | + sock = accept(server) |
| 186 | + global conn = JSONRPC.JSONRPCEndpoint(sock, sock) |
| 187 | + global msg_dispatcher = JSONRPC.MsgDispatcher() |
| 188 | + |
| 189 | + run(conn) |
| 190 | + |
| 191 | + for msg in conn |
| 192 | + @test_throws ErrorException("The handler for the 'request2' request returned a value of type $Int, which is not a valid return type according to the request definition.") my_dispatcher2(conn, msg) |
| 193 | + end |
| 194 | + catch err |
| 195 | + Base.display_error(stderr, err, catch_backtrace()) |
| 196 | + end |
| 197 | + |
| 198 | + wait(server_is_up) |
| 199 | + |
| 200 | + sock2 = connect(global_socket_name2) |
| 201 | + conn2 = JSONRPCEndpoint(sock2, sock2) |
| 202 | + |
| 203 | + run(conn2) |
| 204 | + |
| 205 | + @test_throws JSONRPC.JSONRPCError(-32603, "The handler for the 'request2' request returned a value of type $Int, which is not a valid return type according to the request definition.", nothing) JSONRPC.send(conn2, request2_type, nothing) |
| 206 | + |
| 207 | + close(conn2) |
| 208 | + close(sock2) |
| 209 | + close(conn) |
| 210 | + |
| 211 | + fetch(server_task) |
| 212 | + |
| 213 | +end |
0 commit comments