internal MockClientFactory(MockClient client) { clients = new List <MockClient> { client }; MockCalls = new List <Tuple <string, int> >(); }
public async Task PollReusesMasterServerResponseWhenLastServerUnchanged() { masterServerClient.ServerResponse = MasterServerResponse(); serverInfoClient.ServerResponse = ServerInfoResponse(); _ = await monitor.Poll(server); clientFactory.Reset(); using (MockClient secondServerInfoClient = new MockClient()) { secondServerInfoClient.ServerResponse = ServerInfoResponse(); clientFactory.AddClient(secondServerInfoClient); clock.CurrentTime += TimeSpan.FromSeconds(60); ServerInfo info = await monitor.Poll(server); Assert.AreEqual("4.3.2.1:4660", info.Address); Assert.AreEqual("SERVER-NAME", info.Name); Assert.AreEqual(23, info.NumPlayers); Assert.AreEqual(42, info.MaxPlayers); Assert.AreEqual(1, clientFactory.MockCalls.Count); Assert.AreEqual("4.3.2.1", clientFactory.MockCalls[0].Item1); Assert.AreEqual(4660, clientFactory.MockCalls[0].Item2); CollectionAssert.AreEqual( new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0x54, 0x53, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x20, 0x45, 0x6E, 0x67, 0x69, 0x6E, 0x65, 0x20, 0x51, 0x75, 0x65, 0x72, 0x79, 0x00 }, secondServerInfoClient.ServerRequest); } }
public async Task PollDoesNotReuseGuessedGameServerPort() { masterServerClient.ServerResponse = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0x66, 0x0A, // Header 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // End }; serverInfoClient.ServerResponse = ServerInfoResponse(); _ = await monitor.Poll(server, source); clientFactory.Reset(); using (MockClient secondMasterServerClient = new MockClient(), secondServerInfoClient = new MockClient()) { secondMasterServerClient.ServerResponse = MasterServerResponse( new byte[] { 0x02, 0x03, 0x04, 0x05, 0x11, 0x22 }); clientFactory.AddClient(secondMasterServerClient); secondServerInfoClient.ServerResponse = ServerInfoResponse(); clientFactory.AddClient(secondServerInfoClient); clock.CurrentTime += TimeSpan.FromMinutes(10); ServerInfo info = await monitor.Poll(server, source); Assert.AreEqual(2, clientFactory.MockCalls.Count); Assert.AreEqual("hl2master.steampowered.com", clientFactory.MockCalls[0].Item1); Assert.AreEqual(27011, clientFactory.MockCalls[0].Item2); Assert.AreEqual("2.3.4.5", clientFactory.MockCalls[1].Item1); Assert.AreEqual(4386, clientFactory.MockCalls[1].Item2); Assert.IsNotNull(info); } }
public async Task PollDoesNotSaveMoreThanOneMasterServerResponse() { masterServerClient.ServerResponse = MasterServerResponse(); serverInfoClient.ServerResponse = ServerInfoResponse(); clientFactory.AddClient(masterServerClient); clientFactory.AddClient(serverInfoClient); _ = await monitor.Poll(server, source); _ = await monitor.Poll(new Server("5.5.5.5", 5555), source); clientFactory.Reset(); using (MockClient thirdMasterServerClient = new MockClient(), thirdServerInfoClient = new MockClient()) { thirdMasterServerClient.ServerResponse = MasterServerResponse( new byte[] { 0x09, 0x08, 0x07, 0x06, 0x76, 0x54 }); clientFactory.AddClient(thirdMasterServerClient); thirdServerInfoClient.ServerResponse = ServerInfoResponse(); clientFactory.AddClient(thirdServerInfoClient); ServerInfo info = await monitor.Poll(server, source); Assert.AreEqual(2, clientFactory.MockCalls.Count); Assert.AreEqual("hl2master.steampowered.com", clientFactory.MockCalls[0].Item1); Assert.AreEqual(27011, clientFactory.MockCalls[0].Item2); Assert.AreEqual("9.8.7.6", clientFactory.MockCalls[1].Item1); Assert.AreEqual(30292, clientFactory.MockCalls[1].Item2); } }
public IClient Create(string host, int port) { MockCalls.Add(new Tuple <string, int>(host, port)); MockClient client = clients[0]; clients.RemoveAt(0); return(client); }
public void Initialize() { server = new Server("1.2.3.4", 5678); clock = new MockClock(); clock.CurrentTime += TimeSpan.FromSeconds(100); masterServerClient = new MockClient(); serverInfoClient = new MockClient(); clientFactory = new MockClientFactory(masterServerClient); clientFactory.AddClient(serverInfoClient); monitor = new Monitor(clock, clientFactory, new MockLogger()); }
public async Task PollDoesQueryIfServerChangedBut60SecondsHasNotElapsed() { masterServerClient.ServerResponse = MasterServerResponse(); serverInfoClient.ServerResponse = ServerInfoResponse(); _ = await monitor.Poll(server, source); clientFactory.Reset(); using (MockClient secondServerInfoClient = new MockClient(), secondMasterServerClient = new MockClient()) { secondMasterServerClient.ServerResponse = MasterServerResponse(); secondServerInfoClient.ServerResponse = ServerInfoResponse(); clientFactory.AddClient(secondMasterServerClient); clientFactory.AddClient(secondServerInfoClient); ServerInfo info = await monitor.Poll(new Server("9.9.9.9", 8888), source); Assert.AreEqual("4.3.2.1:4660", info.Address); Assert.AreEqual("SERVER-NAME", info.Name); Assert.AreEqual(23, info.NumPlayers); Assert.AreEqual(42, info.MaxPlayers); Assert.AreEqual(2, clientFactory.MockCalls.Count); Assert.AreEqual("hl2master.steampowered.com", clientFactory.MockCalls[0].Item1); Assert.AreEqual(27011, clientFactory.MockCalls[0].Item2); Assert.AreEqual("4.3.2.1", clientFactory.MockCalls[1].Item1); Assert.AreEqual(4660, clientFactory.MockCalls[1].Item2); CollectionAssert.AreEqual( new byte[] { 0x31, 0xFF, 0x30, 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x30, 0x3A, 0x30, 0x00, 0x5C, 0x67, 0x61, 0x6D, 0x65, 0x5C, 0x44, 0x61, 0x79, 0x5A, 0x5C, 0x67, 0x61, 0x6D, 0x65, 0x61, 0x64, 0x64, 0x72, 0x5C, 0x39, 0x2E, 0x39, 0x2E, 0x39, 0x2E, 0x39, 0x3A, 0x38, 0x38, 0x38, 0x38, 0x00 }, secondMasterServerClient.ServerRequest); CollectionAssert.AreEqual( new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0x54, 0x53, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x20, 0x45, 0x6E, 0x67, 0x69, 0x6E, 0x65, 0x20, 0x51, 0x75, 0x65, 0x72, 0x79, 0x00 }, secondServerInfoClient.ServerRequest); } }
public async Task PollSerializesConcurrentCalls() { masterServerClient.ServerResponse = MasterServerResponse(); serverInfoClient.ServerResponse = ServerInfoResponse(); using (MockClient secondServerInfoClient = new MockClient()) { secondServerInfoClient.ServerResponse = ServerInfoResponse(); serverInfoClient.RequestAction = () => { Assert.AreEqual(2, clientFactory.MockCalls.Count); clientFactory.AddClient(secondServerInfoClient); clock.CurrentTime += TimeSpan.FromSeconds(60); }; Task <ServerInfo> firstPollTask = monitor.Poll(server); Task <ServerInfo> secondPollTask = monitor.Poll(server); _ = await firstPollTask; _ = await secondPollTask; Assert.AreEqual(3, clientFactory.MockCalls.Count); } }
internal void AddClient(MockClient client) { clients.Add(client); }