DefaultJobBundleFactory implements JobBundleFactory { @Override public StageBundleFactory forStage(ExecutableStage executableStage) { return new SimpleStageBundleFactory(executableStage); } DefaultJobBundleFactory(
      JobInfo jobInfo, Map<String, EnvironmentFactory.Provider> environmentFactoryMap); @VisibleForTesting  DefaultJobBundleFactory(
      JobInfo jobInfo,
      Map<String, EnvironmentFactory.Provider> environmentFactoryMap,
      IdGenerator stageIdGenerator,
      ServerInfo serverInfo); static DefaultJobBundleFactory create(JobInfo jobInfo); static DefaultJobBundleFactory create(
      JobInfo jobInfo, Map<String, EnvironmentFactory.Provider> environmentFactoryProviderMap); @Override StageBundleFactory forStage(ExecutableStage executableStage); @Override synchronized void close();  }
@Test public void createsCorrectEnvironment() throws Exception { try (DefaultJobBundleFactory bundleFactory = createDefaultJobBundleFactory(envFactoryProviderMap)) { bundleFactory.forStage(getExecutableStage(environment)); verify(envFactory).createEnvironment(eq(environment), any()); } }
@Test public void createsMultipleEnvironmentOfSingleType() throws Exception { ServerFactory serverFactory = ServerFactory.createDefault(); Environment environmentA = Environment.newBuilder() .setUrn("env:urn:a") .setPayload(ByteString.copyFrom(new byte[1])) .build(); Environment environmentAA = Environment.newBuilder() .setUrn("env:urn:a") .setPayload(ByteString.copyFrom(new byte[2])) .build(); EnvironmentFactory envFactoryA = mock(EnvironmentFactory.class); when(envFactoryA.createEnvironment(eq(environmentA), any())).thenReturn(remoteEnvironment); when(envFactoryA.createEnvironment(eq(environmentAA), any())).thenReturn(remoteEnvironment); EnvironmentFactory.Provider environmentProviderFactoryA = mock(EnvironmentFactory.Provider.class); when(environmentProviderFactoryA.createEnvironmentFactory( any(), any(), any(), any(), any(), any())) .thenReturn(envFactoryA); when(environmentProviderFactoryA.getServerFactory()).thenReturn(serverFactory); Environment environmentB = Environment.newBuilder().setUrn("env:urn:b").build(); EnvironmentFactory envFactoryB = mock(EnvironmentFactory.class); when(envFactoryB.createEnvironment(eq(environmentB), any())).thenReturn(remoteEnvironment); EnvironmentFactory.Provider environmentProviderFactoryB = mock(EnvironmentFactory.Provider.class); when(environmentProviderFactoryB.createEnvironmentFactory( any(), any(), any(), any(), any(), any())) .thenReturn(envFactoryB); when(environmentProviderFactoryB.getServerFactory()).thenReturn(serverFactory); Map<String, Provider> environmentFactoryProviderMap = ImmutableMap.of( environmentA.getUrn(), environmentProviderFactoryA, environmentB.getUrn(), environmentProviderFactoryB); try (DefaultJobBundleFactory bundleFactory = createDefaultJobBundleFactory(environmentFactoryProviderMap)) { bundleFactory.forStage(getExecutableStage(environmentA)); verify(environmentProviderFactoryA, Mockito.times(1)) .createEnvironmentFactory(any(), any(), any(), any(), any(), any()); verify(environmentProviderFactoryB, Mockito.times(0)) .createEnvironmentFactory(any(), any(), any(), any(), any(), any()); verify(envFactoryA, Mockito.times(1)).createEnvironment(eq(environmentA), any()); verify(envFactoryA, Mockito.times(0)).createEnvironment(eq(environmentAA), any()); bundleFactory.forStage(getExecutableStage(environmentAA)); verify(environmentProviderFactoryA, Mockito.times(2)) .createEnvironmentFactory(any(), any(), any(), any(), any(), any()); verify(environmentProviderFactoryB, Mockito.times(0)) .createEnvironmentFactory(any(), any(), any(), any(), any(), any()); verify(envFactoryA, Mockito.times(1)).createEnvironment(eq(environmentA), any()); verify(envFactoryA, Mockito.times(1)).createEnvironment(eq(environmentAA), any()); } }
@Test public void cachesEnvironment() throws Exception { try (DefaultJobBundleFactory bundleFactory = createDefaultJobBundleFactory(envFactoryProviderMap)) { StageBundleFactory bf1 = bundleFactory.forStage(getExecutableStage(environment)); StageBundleFactory bf2 = bundleFactory.forStage(getExecutableStage(environment)); System.out.println("bundle factory 1:" + bf1); System.out.println("bundle factory 1:" + bf2); verify(envFactory).createEnvironment(eq(environment), any()); verifyNoMoreInteractions(envFactory); } }
@Test public void doesNotCacheDifferentEnvironments() throws Exception { Environment envFoo = Environment.newBuilder().setUrn("dummy:urn:another").build(); RemoteEnvironment remoteEnvFoo = mock(RemoteEnvironment.class); InstructionRequestHandler fooInstructionHandler = mock(InstructionRequestHandler.class); Map<String, EnvironmentFactory.Provider> envFactoryProviderMapFoo = ImmutableMap.of( environment.getUrn(), envFactoryProvider, envFoo.getUrn(), envFactoryProvider); when(envFactory.createEnvironment(eq(envFoo), any())).thenReturn(remoteEnvFoo); when(remoteEnvFoo.getInstructionRequestHandler()).thenReturn(fooInstructionHandler); when(fooInstructionHandler.handle(any())) .thenReturn(CompletableFuture.completedFuture(instructionResponse)); try (DefaultJobBundleFactory bundleFactory = createDefaultJobBundleFactory(envFactoryProviderMapFoo)) { bundleFactory.forStage(getExecutableStage(environment)); bundleFactory.forStage(getExecutableStage(envFoo)); verify(envFactory).createEnvironment(eq(environment), any()); verify(envFactory).createEnvironment(eq(envFoo), any()); verifyNoMoreInteractions(envFactory); } }