Skip to content
This repository was archived by the owner on Dec 23, 2022. It is now read-only.

Commit 996a6c6

Browse files
committed
Ensure the connection tests correctly assert
1 parent 5f53e53 commit 996a6c6

File tree

1 file changed

+73
-43
lines changed

1 file changed

+73
-43
lines changed
Lines changed: 73 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Net;
1+
using System;
2+
using System.Net;
3+
using System.Threading;
24
using Xunit;
35

46
namespace 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,21 +65,28 @@ 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]
@@ -72,21 +95,28 @@ public void TestCommandSuccess()
7295
var server = new RemoteConServer(IPAddress.Any, 27015);
7396
server.StartListening();
7497

75-
var client = new RemoteConClient();
76-
client.OnAuthResult += success =>
98+
string commandResult = null;
99+
using (var waitEvent = new AutoResetEvent(false))
77100
{
78-
//Assert.True(success);
79-
client.SendCommand("hello", result =>
101+
var client = new RemoteConClient();
102+
client.OnAuthResult += success =>
80103
{
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");
104+
client.SendCommand("hello", result =>
105+
{
106+
commandResult = result;
107+
108+
client.Disconnect();
109+
server.StopListening();
110+
waitEvent.Set();
111+
});
112+
};
113+
114+
client.Connect("127.0.0.1", 27015);
115+
client.Authenticate("changeme");
116+
waitEvent.WaitOne();
117+
}
118+
119+
Assert.Contains("world", commandResult);
90120
}
91121
}
92122
}

0 commit comments

Comments
 (0)