Skip to content

Commit b72e2b0

Browse files
committed
test setup
1 parent 956632b commit b72e2b0

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

test/Docker.Testify/DockerSetup.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public abstract class DockerSetup : IDisposable
3030
protected readonly DockerClient docker;
3131
protected string containerId;
3232

33+
private static object Lock = new object();
34+
3335
protected DockerSetup()
3436
{
3537
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
@@ -122,21 +124,24 @@ public void Dispose()
122124

123125
private int GetFreePort()
124126
{
125-
const int startRange = 1000;
126-
const int endRange = 10000;
127-
var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
128-
var tcpPorts = ipGlobalProperties.GetActiveTcpListeners();
129-
var udpPorts = ipGlobalProperties.GetActiveUdpListeners();
127+
lock (Lock)
128+
{
129+
const int startRange = 1000;
130+
const int endRange = 10000;
131+
var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
132+
var tcpPorts = ipGlobalProperties.GetActiveTcpListeners();
133+
var udpPorts = ipGlobalProperties.GetActiveUdpListeners();
130134

131-
var result = startRange;
135+
var result = startRange;
132136

133-
while (((tcpPorts.Any(x => x.Port == result)) || (udpPorts.Any(x => x.Port == result))) && result <= endRange)
134-
result++;
137+
while (((tcpPorts.Any(x => x.Port == result)) || (udpPorts.Any(x => x.Port == result))) && result <= endRange)
138+
result++;
135139

136-
if (result > endRange)
137-
throw new PortsInUseException();
140+
if (result > endRange)
141+
throw new PortsInUseException();
138142

139-
return result;
143+
return result;
144+
}
140145
}
141146
}
142147
}

0 commit comments

Comments
 (0)