1
+ import 'dart:async' ;
2
+ import 'dart:io' ;
3
+ import 'dart:typed_data' ;
4
+ import 'package:connectme/connectme.dart' ;
5
+ import 'package:serveme/serveme.dart' ;
6
+ import 'package:test/test.dart' ;
7
+ import 'generated/test.generated.dart' ;
8
+ import 'modules/test.dart' ;
9
+
10
+ void main () {
11
+ late ServeMe <ServeMeClient > server;
12
+ late ConnectMeClient client;
13
+ late Timer timer;
14
+ late TestModule module;
15
+
16
+ group ('Connection tests' , () {
17
+ test ('ServeMe.run() and ConnectMe.connect()' , () async {
18
+ timer = Timer (const Duration (seconds: 2 ), () => fail ('Operation timed out' ));
19
+ server = ServeMe <ServeMeClient >(
20
+ configFile: 'test/config_test.yaml' ,
21
+ modules: < String , Module <ServeMeClient >> {
22
+ 'test' : module = TestModule (),
23
+ },
24
+ );
25
+ await server.run ();
26
+ module.events.listen <ConnectEvent <ServeMeClient >>(expectAsync1 <Future <void >, dynamic >((dynamic event) async {
27
+ expect (event.client, isA <ServeMeClient >());
28
+ }));
29
+ module.events.listen <DisconnectEvent <ServeMeClient >>(expectAsync1 <Future <void >, dynamic >((dynamic event) async {
30
+ expect (event.client, isA <ServeMeClient >());
31
+ }));
32
+ client = await ConnectMe .connect ('ws://127.0.0.1:31337' ,
33
+ onConnect: expectAsync0 <void >(() {}),
34
+ );
35
+ await client.close ();
36
+ await server.stop ();
37
+ timer.cancel ();
38
+ });
39
+ });
40
+
41
+ group ('ServeMe data exchange tests' , () {
42
+ setUp (() async {
43
+ timer = Timer (const Duration (seconds: 2 ), () => fail ('Operation timed out' ));
44
+ server = ServeMe <ServeMeClient >(
45
+ configFile: 'test/config_test.yaml' ,
46
+ modules: < String , Module <ServeMeClient >> {
47
+ 'test' : module = TestModule (),
48
+ },
49
+ );
50
+ await server.run ();
51
+ client = await ConnectMe .connect ('ws://127.0.0.1:31337' );
52
+ });
53
+
54
+ tearDown (() async {
55
+ await client.close ();
56
+ await server.stop ();
57
+ timer.cancel ();
58
+ });
59
+
60
+ test ('Client sends String to server' , () async {
61
+ final Completer <String > completer = Completer <String >();
62
+ server.listen <String >((String message, ConnectMeClient client) async {
63
+ completer.complete (message);
64
+ });
65
+ client.send ('Test message from client' );
66
+ expect (await completer.future, 'Test message from client' );
67
+ });
68
+
69
+ test ('Server broadcasts String to clients' , () async {
70
+ final Completer <String > completer = Completer <String >();
71
+ client.listen <String >((String message) {
72
+ completer.complete (message);
73
+ });
74
+ server.broadcast ('Test message from server' );
75
+ expect (await completer.future, 'Test message from server' );
76
+ });
77
+
78
+ test ('Client sends Uint8List to server' , () async {
79
+ final Completer <Uint8List > completer = Completer <Uint8List >();
80
+ server.listen <Uint8List >((Uint8List message, ConnectMeClient client) async {
81
+ completer.complete (message);
82
+ });
83
+ client.send (Uint8List .fromList (< int > [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ]));
84
+ expect (await completer.future, Uint8List .fromList (< int > [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ]));
85
+ });
86
+
87
+ test ('Server broadcasts Uint8List to clients' , () async {
88
+ final Completer <Uint8List > completer = Completer <Uint8List >();
89
+ client.listen <Uint8List >((Uint8List message) {
90
+ completer.complete (message);
91
+ });
92
+ server.broadcast (Uint8List .fromList (< int > [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ]));
93
+ expect (await completer.future, Uint8List .fromList (< int > [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ]));
94
+ });
95
+
96
+ test ('Client sends TestResponse query to server' , () async {
97
+ server.register (testMessageFactory);
98
+ server.listen <TestRequest >((TestRequest request, ConnectMeClient client) async {
99
+ client.send (request.$response (responseParam: request.requestParam));
100
+ });
101
+ client.register (testMessageFactory);
102
+ final TestResponse response = await client.query <TestResponse >(TestRequest (requestParam: 3.1415926535 ));
103
+ expect (response.responseParam, 3.1415926535 );
104
+ });
105
+
106
+ test ('Server sends TestResponse query to client' , () async {
107
+ client.register (testMessageFactory);
108
+ client.listen <TestRequest >((TestRequest request) {
109
+ client.send (request.$response (responseParam: request.requestParam));
110
+ });
111
+ server.register (testMessageFactory);
112
+ final TestResponse response = await server.clients.first.query <TestResponse >(TestRequest (requestParam: 3.1415926535 ));
113
+ expect (response.responseParam, 3.1415926535 );
114
+ });
115
+ });
116
+
117
+ group ('ServeMe API tests' , () {
118
+ setUp (() async {
119
+ timer = Timer (const Duration (seconds: 2 ), () => fail ('Operation timed out' ));
120
+ server = ServeMe <ServeMeClient >(
121
+ configFile: 'test/config_test.yaml' ,
122
+ modules: < String , Module <ServeMeClient >> {
123
+ 'test' : module = TestModule (),
124
+ },
125
+ );
126
+ await server.run ();
127
+ });
128
+
129
+ tearDown (() async {
130
+ await server.stop ();
131
+ timer.cancel ();
132
+ });
133
+
134
+ test ('TickEvent dispatch and handle' , () async {
135
+ module.events.listen <TickEvent >(expectAsync1 <Future <void >, dynamic >((dynamic event) async {
136
+ expect (event, isA <TickEvent >());
137
+ }));
138
+ });
139
+
140
+ test ('Scheduler' , () async {
141
+ final Task task = Task (DateTime .now (), expectAsync1 <Future <void >, dynamic >((dynamic time) async {
142
+ expect (time, isA <DateTime >());
143
+ }));
144
+ module.scheduler.schedule (task);
145
+ });
146
+ });
147
+ }
0 commit comments