Improve network test flakiness

This commit is contained in:
Ben Olden-Cooligan 2022-07-02 12:07:12 -07:00
parent e7e368a87d
commit e107fc663b
2 changed files with 28 additions and 15 deletions

View File

@ -15,12 +15,11 @@ public class ServerDiscoveryTests
ServerName = "NetworkScanTests.ServerDiscovery"
});
var client = new NetworkScanClient();
server.Start();
await Task.Delay(100);
await server.Start();
var discovered = await client.DiscoverServers(100);
Assert.Contains(discovered, x => x.Name == "NetworkScanTests.ServerDiscovery");
}
[Fact]
public async Task ServerDiscoveryCustomPort()
{
@ -33,12 +32,11 @@ public class ServerDiscoveryTests
{
DiscoveryPort = 33433
});
server.Start();
await Task.Delay(100);
await server.Start();
var discovered = await client.DiscoverServers(100);
Assert.Contains(discovered, x => x.Name == "NetworkScanTests.ServerDiscoveryCustomPort");
}
[Fact]
public async Task ServerDiscoveryMismatchPort()
{
@ -51,12 +49,11 @@ public class ServerDiscoveryTests
{
DiscoveryPort = 33555
});
server.Start();
await Task.Delay(100);
await server.Start();
var discovered = await client.DiscoverServers(100);
Assert.DoesNotContain(discovered, x => x.Name == "NetworkScanTests.ServerDiscoveryMismatchPort");
}
[Fact]
public async Task ServerDiscoveryOff()
{
@ -66,8 +63,7 @@ public class ServerDiscoveryTests
AllowDiscovery = false
});
var client = new NetworkScanClient();
server.Start();
await Task.Delay(100);
await server.Start();
var discovered = await client.DiscoverServers(100);
Assert.DoesNotContain(discovered, x => x.Name == "NetworkScanTests.ServerDiscoveryOff");
}

View File

@ -24,14 +24,15 @@ public class NetworkScanServer : IDisposable
{
}
internal NetworkScanServer(ScanningContext scanningContext, IScanBridgeFactory scanBridgeFactory, NetworkScanServerOptions options)
internal NetworkScanServer(ScanningContext scanningContext, IScanBridgeFactory scanBridgeFactory,
NetworkScanServerOptions options)
{
_scanningContext = scanningContext;
_options = options;
_scanBridgeFactory = scanBridgeFactory;
}
public void Start()
public Task Start()
{
if (_server != null)
{
@ -42,7 +43,11 @@ public class NetworkScanServer : IDisposable
// TODO: Secure
_server = new Server
{
Services = { NetworkScanService.BindService(new NetworkScanServiceImpl(_scanningContext.ImageContext, _scanBridgeFactory)) },
Services =
{
NetworkScanService.BindService(new NetworkScanServiceImpl(_scanningContext.ImageContext,
_scanBridgeFactory))
},
Ports = { new ServerPort("0.0.0.0", _options.Port ?? ServerPort.PickUnused, ServerCredentials.Insecure) }
};
_server.Start();
@ -53,8 +58,20 @@ public class NetworkScanServer : IDisposable
int discoveryPort = _options.DiscoveryPort ?? Discovery.DEFAULT_DISCOVERY_PORT;
int serverPort = _server.Ports.Single().BoundPort;
string serverName = _options.ServerName ?? Environment.MachineName;
Task.Run(() => Discovery.ListenForBroadcast(discoveryPort, serverPort, serverName, _cts.Token));
var tcs = new TaskCompletionSource<bool>();
Task.Run(() =>
{
// We only want Start to resolve once this task starts running. Otherwise discovery might not yet be
// active which could cause test failures. Of course there's still a few ms until the actual UDP socket
// receive starts, but that shouldn't generally be an issue. We could also consider tying this to
// UdpClient.BeginReceive resolving though that's more complicated.
tcs.TrySetResult(true);
return Discovery.ListenForBroadcast(discoveryPort, serverPort, serverName, _cts.Token);
});
return tcs.Task;
}
return Task.CompletedTask;
}
public void Kill()