1- using System . Net ;
1+ using System ;
2+ using System . Net ;
3+ using System . Threading ;
24using Xunit ;
35
46namespace RCONServerLib . Tests
@@ -11,17 +13,23 @@ public void TestAuthFail()
1113 var server = new RemoteConServer ( IPAddress . Any , 27015 ) ;
1214 server . StartListening ( ) ;
1315
14- var client = new RemoteConClient ( ) ;
15- client . OnAuthResult += success =>
16+ bool authResult = false ;
17+ using ( var waitEvent = new AutoResetEvent ( false ) )
1618 {
17- Assert . False ( success ) ;
19+ var client = new RemoteConClient ( ) ;
20+ client . OnAuthResult += success =>
21+ {
22+ authResult = success ;
23+
24+ client . Disconnect ( ) ;
25+ server . StopListening ( ) ;
26+ } ;
1827
19- client . Disconnect ( ) ;
20- server . StopListening ( ) ;
21- } ;
28+ client . Connect ( "127.0.0.1" , 27015 ) ;
29+ client . Authenticate ( "unitfail" ) ;
30+ }
2231
23- client . Connect ( "127.0.0.1" , 27015 ) ;
24- client . Authenticate ( "unitfail" ) ;
32+ Assert . False ( authResult ) ;
2533 }
2634
2735 [ Fact ]
@@ -30,17 +38,25 @@ public void TestAuthSuccess()
3038 var server = new RemoteConServer ( IPAddress . Any , 27015 ) ;
3139 server . StartListening ( ) ;
3240
33- var client = new RemoteConClient ( ) ;
34- client . OnAuthResult += success =>
41+ bool authResult = false ;
42+ using ( var waitEvent = new AutoResetEvent ( false ) )
3543 {
36- Assert . True ( success ) ;
44+ var client = new RemoteConClient ( ) ;
45+ client . OnAuthResult += success =>
46+ {
47+ authResult = success ;
48+
49+ client . Disconnect ( ) ;
50+ server . StopListening ( ) ;
51+ waitEvent . Set ( ) ;
52+ } ;
3753
38- client . Disconnect ( ) ;
39- server . StopListening ( ) ;
40- } ;
54+ client . Connect ( "127.0.0.1" , 27015 ) ;
55+ client . Authenticate ( "changeme" ) ;
56+ waitEvent . WaitOne ( ) ;
57+ }
4158
42- client . Connect ( "127.0.0.1" , 27015 ) ;
43- client . Authenticate ( "changeme" ) ;
59+ Assert . True ( authResult ) ;
4460 }
4561
4662 [ Fact ]
@@ -49,44 +65,59 @@ public void TestCommandFail()
4965 var server = new RemoteConServer ( IPAddress . Any , 27015 ) ;
5066 server . StartListening ( ) ;
5167
52- var client = new RemoteConClient ( ) ;
53- client . OnAuthResult += success =>
68+ string commandResult = null ;
69+ using ( var waitEvent = new AutoResetEvent ( false ) )
5470 {
55- //Assert.True(success );
56- client . SendCommand ( "testing" , result =>
71+ var client = new RemoteConClient ( ) ;
72+ client . OnAuthResult += success =>
5773 {
58- Assert . Contains ( "invalid command" , result ) ;
59-
60- client . Disconnect ( ) ;
61- server . StopListening ( ) ;
62- } ) ;
63- } ;
64-
65- client . Connect ( "127.0.0.1" , 27015 ) ;
66- client . Authenticate ( "changeme" ) ;
74+ client . SendCommand ( "testing" , result =>
75+ {
76+ commandResult = result ;
77+
78+ client . Disconnect ( ) ;
79+ server . StopListening ( ) ;
80+ waitEvent . Set ( ) ;
81+ } ) ;
82+ } ;
83+
84+ client . Connect ( "127.0.0.1" , 27015 ) ;
85+ client . Authenticate ( "changeme" ) ;
86+ waitEvent . WaitOne ( ) ;
87+ }
88+
89+ Assert . Contains ( "Invalid command" , commandResult ) ;
6790 }
6891
6992 [ Fact ]
7093 public void TestCommandSuccess ( )
7194 {
7295 var server = new RemoteConServer ( IPAddress . Any , 27015 ) ;
96+ server . CommandManager . Add ( "hello" , "Replies with world" , ( command , args ) => "world" ) ;
7397 server . StartListening ( ) ;
7498
75- var client = new RemoteConClient ( ) ;
76- client . OnAuthResult += success =>
99+ string commandResult = null ;
100+ using ( var waitEvent = new AutoResetEvent ( false ) )
77101 {
78- //Assert.True(success );
79- client . SendCommand ( "hello" , result =>
102+ var client = new RemoteConClient ( ) ;
103+ client . OnAuthResult += success =>
80104 {
81- Assert . Contains ( "world" , result ) ;
82-
83- client . Disconnect ( ) ;
84- server . StopListening ( ) ;
85- } ) ;
86- } ;
87-
88- client . Connect ( "127.0.0.1" , 27015 ) ;
89- client . Authenticate ( "changeme" ) ;
105+ client . SendCommand ( "hello" , result =>
106+ {
107+ commandResult = result ;
108+
109+ client . Disconnect ( ) ;
110+ server . StopListening ( ) ;
111+ waitEvent . Set ( ) ;
112+ } ) ;
113+ } ;
114+
115+ client . Connect ( "127.0.0.1" , 27015 ) ;
116+ client . Authenticate ( "changeme" ) ;
117+ waitEvent . WaitOne ( ) ;
118+ }
119+
120+ Assert . Contains ( "world" , commandResult ) ;
90121 }
91122 }
92123}
0 commit comments