21
21
22
22
import com .google .common .base .Joiner ;
23
23
import com .google .common .collect .ImmutableMap ;
24
+ import com .google .common .collect .Lists ;
24
25
import com .google .devtools .build .lib .actions .Spawn ;
26
+ import com .google .devtools .build .lib .actions .UserExecException ;
25
27
import com .google .devtools .build .lib .vfs .DigestHashFunction ;
26
28
import com .google .devtools .build .lib .vfs .FileSystem ;
27
29
import com .google .devtools .build .lib .vfs .FileSystemUtils ;
31
33
import com .google .devtools .build .lib .worker .WorkerProtocol .WorkRequest ;
32
34
import java .io .IOException ;
33
35
import java .io .PipedInputStream ;
36
+ import java .util .ArrayList ;
34
37
import org .junit .Before ;
35
38
import org .junit .Test ;
36
39
import org .junit .runner .RunWith ;
@@ -58,8 +61,12 @@ public void setUp() throws IOException {
58
61
59
62
@ Test
60
63
public void prepareExecution_createsFilesInSandbox () throws IOException , InterruptedException {
61
- SandboxedWorkerProxy proxy = createSandboxedWorkerProxy ();
62
- Path workDir = workerBaseDir .getChild ("Mnem-multiplex-worker-workdir" ).getChild ("execroot" );
64
+ SandboxedWorkerProxy proxy = createSandboxedWorkerProxies ("Mnem" , 1 ).get (0 );
65
+ int multiplexerId = proxy .workerMultiplexer .getMultiplexerId ();
66
+ Path workDir =
67
+ workerBaseDir
68
+ .getChild ("Mnem-multiplex-worker-" + multiplexerId + "-workdir" )
69
+ .getChild ("execroot" );
63
70
Path sandboxDir =
64
71
workDir
65
72
.getChild ("__sandbox" )
@@ -94,7 +101,11 @@ public void prepareExecution_createsFilesInSandbox() throws IOException, Interru
94
101
@ Test
95
102
public void putRequest_setsSandboxDir () throws IOException , InterruptedException {
96
103
SandboxedWorkerProxy worker = createFakedSandboxedWorkerProxy ();
97
- Path workDir = workerBaseDir .getChild ("Mnem-multiplex-worker-workdir" ).getChild ("execroot" );
104
+ int multiplexerId = worker .workerMultiplexer .getMultiplexerId ();
105
+ Path workDir =
106
+ workerBaseDir
107
+ .getChild ("Mnem-multiplex-worker-" + multiplexerId + "-workdir" )
108
+ .getChild ("execroot" );
98
109
SandboxHelper sandboxHelper =
99
110
new SandboxHelper (globalExecRoot , workDir )
100
111
.addAndCreateInputFile ("anInputFile" , "anInputFile" , "Just stuff" )
@@ -115,7 +126,11 @@ public void putRequest_setsSandboxDir() throws IOException, InterruptedException
115
126
@ Test
116
127
public void finishExecution_copiesOutputs () throws IOException , InterruptedException {
117
128
SandboxedWorkerProxy worker = createFakedSandboxedWorkerProxy ();
118
- Path workDir = workerBaseDir .getChild ("Mnem-multiplex-worker-workdir" ).getChild ("execroot" );
129
+ int multiplexerId = worker .workerMultiplexer .getMultiplexerId ();
130
+ Path workDir =
131
+ workerBaseDir
132
+ .getChild ("Mnem-multiplex-worker-" + multiplexerId + "-workdir" )
133
+ .getChild ("execroot" );
119
134
SandboxHelper sandboxHelper =
120
135
new SandboxHelper (globalExecRoot , workDir )
121
136
.addAndCreateInputFile ("anInputFile" , "anInputFile" , "Just stuff" )
@@ -150,8 +165,37 @@ public void finishExecution_copiesOutputs() throws IOException, InterruptedExcep
150
165
.isEqualTo ("some output" );
151
166
}
152
167
153
- private SandboxedWorkerProxy createSandboxedWorkerProxy () throws IOException {
154
- ImmutableMap .Builder <String , String > req = TestUtils .execRequirementsBuilder ("Mnem" );
168
+ @ Test
169
+ public void differentProxiesSameMultiplexerHaveSameWorkDir ()
170
+ throws IOException , InterruptedException {
171
+ ArrayList <SandboxedWorkerProxy > proxies = createSandboxedWorkerProxies ("Mnem" , 2 );
172
+ SandboxedWorkerProxy proxyOne = proxies .get (0 );
173
+ SandboxedWorkerProxy proxyTwo = proxies .get (1 );
174
+
175
+ int multiplexerIdProxyOne = proxyOne .workerMultiplexer .getMultiplexerId ();
176
+ Path expectedWorkDirProxyOne =
177
+ workerBaseDir
178
+ .getChild ("Mnem-multiplex-worker-" + multiplexerIdProxyOne + "-workdir" )
179
+ .getChild ("execroot" );
180
+
181
+ int multiplexerIdProxyTwo = proxyTwo .workerMultiplexer .getMultiplexerId ();
182
+ Path expectedWorkDirProxyTwo =
183
+ workerBaseDir
184
+ .getChild ("Mnem-multiplex-worker-" + multiplexerIdProxyTwo + "-workdir" )
185
+ .getChild ("execroot" );
186
+
187
+ assertThat (proxyOne .workDir ).isEqualTo (proxyTwo .workDir );
188
+ assertThat (proxyOne .workDir ).isEqualTo (expectedWorkDirProxyOne );
189
+ assertThat (proxyTwo .workDir ).isEqualTo (expectedWorkDirProxyTwo );
190
+ }
191
+
192
+ @ Test
193
+ public void differentProxiesDifferentMultiplexerSameMnemHaveDifferentWorkDirs ()
194
+ throws IOException , InterruptedException , UserExecException {
195
+ String sharedMnemonic = "Mnem" ;
196
+
197
+ // Create a proxy on the first multiplexer
198
+ ImmutableMap .Builder <String , String > req = TestUtils .execRequirementsBuilder (sharedMnemonic );
155
199
req .put (SUPPORTS_MULTIPLEX_SANDBOXING , "1" );
156
200
Spawn spawn = TestUtils .createSpawn (req .buildOrThrow ());
157
201
@@ -163,7 +207,51 @@ private SandboxedWorkerProxy createSandboxedWorkerProxy() throws IOException {
163
207
TestUtils .createWorkerKeyFromOptions (
164
208
PROTO , globalOutputBase , options , true , spawn , "worker.sh" );
165
209
WorkerFactory factory = new WorkerFactory (workerBaseDir );
166
- return (SandboxedWorkerProxy ) factory .create (key );
210
+
211
+ SandboxedWorkerProxy proxyOneMultiplexerOne = (SandboxedWorkerProxy ) factory .create (key );
212
+ int multiplexerIdOne = proxyOneMultiplexerOne .workerMultiplexer .getMultiplexerId ();
213
+ Path expectedWorkDirOne =
214
+ workerBaseDir
215
+ .getChild (sharedMnemonic + "-multiplex-worker-" + multiplexerIdOne + "-workdir" )
216
+ .getChild ("execroot" );
217
+
218
+ // Shut down the first multiplexer, so we get a different multiplexer for the next proxy
219
+ WorkerMultiplexerManager .removeInstance (key );
220
+
221
+ // Create a proxy on the second multiplexer
222
+ SandboxedWorkerProxy proxyOneMultiplexerTwo = (SandboxedWorkerProxy ) factory .create (key );
223
+ int multiplexerIdTwo = proxyOneMultiplexerTwo .workerMultiplexer .getMultiplexerId ();
224
+ Path expectedWorkDirTwo =
225
+ workerBaseDir
226
+ .getChild (sharedMnemonic + "-multiplex-worker-" + multiplexerIdTwo + "-workdir" )
227
+ .getChild ("execroot" );
228
+
229
+ assertThat (proxyOneMultiplexerOne .workDir ).isNotEqualTo (proxyOneMultiplexerTwo .workDir );
230
+ assertThat (proxyOneMultiplexerOne .workDir ).isEqualTo (expectedWorkDirOne );
231
+ assertThat (proxyOneMultiplexerTwo .workDir ).isEqualTo (expectedWorkDirTwo );
232
+ }
233
+
234
+ private ArrayList <SandboxedWorkerProxy > createSandboxedWorkerProxies (
235
+ String mnemonic , int numProxiesToCreate ) throws IOException {
236
+ ImmutableMap .Builder <String , String > req = TestUtils .execRequirementsBuilder (mnemonic );
237
+ req .put (SUPPORTS_MULTIPLEX_SANDBOXING , "1" );
238
+ Spawn spawn = TestUtils .createSpawn (req .buildOrThrow ());
239
+
240
+ WorkerOptions options = new WorkerOptions ();
241
+ options .workerMultiplex = true ;
242
+ options .multiplexSandboxing = true ;
243
+
244
+ WorkerKey key =
245
+ TestUtils .createWorkerKeyFromOptions (
246
+ PROTO , globalOutputBase , options , true , spawn , "worker.sh" );
247
+ WorkerFactory factory = new WorkerFactory (workerBaseDir );
248
+
249
+ assertThat (numProxiesToCreate ).isGreaterThan (0 );
250
+ ArrayList <SandboxedWorkerProxy > proxies = Lists .newArrayListWithCapacity (numProxiesToCreate );
251
+ for (int i = 0 ; i < numProxiesToCreate ; i ++) {
252
+ proxies .add ((SandboxedWorkerProxy ) factory .create (key ));
253
+ }
254
+ return proxies ;
167
255
}
168
256
169
257
private SandboxedWorkerProxy createFakedSandboxedWorkerProxy () throws IOException {
@@ -180,7 +268,7 @@ private SandboxedWorkerProxy createFakedSandboxedWorkerProxy() throws IOExceptio
180
268
PROTO , globalOutputBase , options , true , spawn , "worker.sh" );
181
269
WorkerMultiplexerManager .injectForTesting (
182
270
key ,
183
- new WorkerMultiplexer (globalExecRoot .getChild ("testWorker.log" ), key ) {
271
+ new WorkerMultiplexer (globalExecRoot .getChild ("testWorker.log" ), key , 0 ) {
184
272
@ Override
185
273
public synchronized void createProcess (Path workDir ) throws IOException {
186
274
PipedInputStream serverInputStream = new PipedInputStream ();
0 commit comments