Skip to content

Commit 37b7079

Browse files
committed
Add tests for custom serialization
1 parent 99b0de7 commit 37b7079

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

test/shared_test_code.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,35 @@
1818
Base.:(==)(a::Foo2,b::Foo2) = a.fieldA == b.fieldA && a.fieldB == b.fieldB
1919

2020
end
21+
22+
@testmodule NamedPipes begin
23+
using Sockets, JSONRPC
24+
25+
export get_named_pipe
26+
27+
function get_named_pipe()
28+
socket_name = JSONRPC.generate_pipe_name()
29+
30+
server_is_up = Base.Event()
31+
32+
socket1_channel = Channel(1)
33+
34+
@async try
35+
server = listen(socket_name)
36+
notify(server_is_up)
37+
socket1 = accept(server)
38+
39+
put!(socket1_channel, socket1)
40+
catch err
41+
Base.display_error(err, catch_backtrace())
42+
end
43+
44+
wait(server_is_up)
45+
46+
socket2 = connect(socket_name)
47+
48+
socket1 = take!(socket1_channel)
49+
50+
return socket1, socket2
51+
end
52+
end

test/test_json_serialization.jl

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
@testitem "Custom JSON serialization" setup=[NamedPipes] begin
2+
using JSON
3+
using JSON: StructuralContext, begin_object, show_pair, end_object, show_json, Serializations.StandardSerialization
4+
5+
struct OurSerialization <: JSON.Serializations.CommonSerialization end
6+
7+
struct OurStruct
8+
a::String
9+
b::String
10+
end
11+
12+
function JSON.show_json(io::StructuralContext, s::OurSerialization, f::OurStruct)
13+
show_json(io, StandardSerialization(), "$(f.a):$(f.b)")
14+
end
15+
16+
x = OurStruct("Hello", "World")
17+
18+
socket1, socket2 = NamedPipes.get_named_pipe()
19+
20+
task_done = Base.Event()
21+
22+
messages_back = Channel(Inf)
23+
24+
@async try
25+
ep2 = JSONRPCEndpoint(socket1, socket1, nothing, OurSerialization())
26+
27+
run(ep2)
28+
29+
msg = JSONRPC.get_next_message(ep2)
30+
put!(messages_back, msg)
31+
32+
msg2 = JSONRPC.get_next_message(ep2)
33+
put!(messages_back, msg2)
34+
send_success_response(ep2, msg2, [x])
35+
36+
msg3 = JSONRPC.get_next_message(ep2)
37+
put!(messages_back, msg3)
38+
send_error_response(ep2, msg3, 5, "Error", [x])
39+
40+
close(ep2)
41+
finally
42+
notify(task_done)
43+
catch err
44+
Base.display_error(err, catch_backtrace())
45+
end
46+
47+
ep1 = JSONRPCEndpoint(socket2, socket2, nothing, OurSerialization())
48+
49+
run(ep1)
50+
51+
send_notification(ep1, "foo", [x])
52+
53+
response1 = send_request(ep1, "bar", [x])
54+
try
55+
send_request(ep1, "bar", [x])
56+
catch err_msg
57+
if err_msg isa JSONRPC.JSONRPCError
58+
@test err_msg.data == Any["Hello:World"]
59+
else
60+
rethrow(err_msg)
61+
end
62+
end
63+
64+
close(ep1)
65+
66+
wait(task_done)
67+
68+
msg1 = take!(messages_back)
69+
msg2 = take!(messages_back)
70+
msg3 = take!(messages_back)
71+
72+
@test msg1.params == ["Hello:World"]
73+
@test msg2.params == ["Hello:World"]
74+
@test msg3.params == ["Hello:World"]
75+
end

0 commit comments

Comments
 (0)