Skip to content

Commit

Permalink
Use unit test execution context for java-client unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauernfeind committed Sep 7, 2022
1 parent d0cba48 commit 92f7c51
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,8 @@ public Collection<String> getImportStrings() {
public void updateVersionString() {
versionString = UuidCreator.toString(UuidCreator.getRandomBased());
}

public void updateVersionString(String version) {
versionString = version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.deephaven.io.log.LogLevel;
import io.deephaven.io.logger.LogBuffer;
import io.deephaven.io.logger.LogBufferOutputStream;
import io.deephaven.server.console.SessionToExecutionStateModule;
import io.deephaven.server.console.groovy.GroovyConsoleSessionModule;
import io.deephaven.server.console.python.PythonConsoleSessionModule;
import io.deephaven.server.console.python.PythonGlobalScopeModule;
Expand Down Expand Up @@ -48,6 +49,7 @@ public class EmbeddedServer {
JettyServerModule.class,
PythonConsoleSessionModule.class,
GroovyConsoleSessionModule.class,
SessionToExecutionStateModule.class,
})
public interface PythonServerComponent extends DeephavenApiServerComponent {
@Component.Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import dagger.BindsInstance;
import dagger.Component;
import io.deephaven.server.console.SessionToExecutionStateModule;
import io.deephaven.server.console.groovy.GroovyConsoleSessionModule;
import io.deephaven.server.console.python.PythonConsoleSessionModule;
import io.deephaven.server.console.python.PythonGlobalScopeCopyModule;
Expand All @@ -28,6 +29,7 @@
JettyServerModule.class,
PythonConsoleSessionModule.class,
GroovyConsoleSessionModule.class,
SessionToExecutionStateModule.class,
})
public interface JettyServerComponent extends DeephavenApiServerComponent {
@Component.Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import dagger.BindsInstance;
import dagger.Component;
import io.deephaven.server.console.SessionToExecutionStateModule;
import io.deephaven.server.console.groovy.GroovyConsoleSessionModule;
import io.deephaven.server.console.python.PythonConsoleSessionModule;
import io.deephaven.server.console.python.PythonGlobalScopeCopyModule;
Expand All @@ -28,6 +29,7 @@
NettyServerModule.class,
PythonConsoleSessionModule.class,
GroovyConsoleSessionModule.class,
SessionToExecutionStateModule.class,
})
public interface NettyServerComponent extends DeephavenApiServerComponent {
@Component.Builder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending
*/
package io.deephaven.server.console;

import dagger.Module;
import dagger.Provides;
import dagger.multibindings.IntoMap;
import dagger.multibindings.StringKey;
import io.deephaven.engine.util.NoLanguageDeephavenSession;
import io.deephaven.engine.util.ScriptSession;
import io.deephaven.server.console.groovy.InitScriptsModule;

@Module(includes = InitScriptsModule.ServiceLoader.class)
public class NoConsoleSessionModule {
@Provides
@IntoMap
@StringKey("none")
ScriptSession bindScriptSession(NoLanguageDeephavenSession noLanguageSession) {
return noLanguageSession;
}

@Provides
NoLanguageDeephavenSession bindNoLanguageSession() {
return new NoLanguageDeephavenSession();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.deephaven.server.console;

import dagger.Module;
import dagger.Provides;
import io.deephaven.engine.context.ExecutionContext;
import io.deephaven.engine.util.ScriptSession;

@Module
public class SessionToExecutionStateModule {
@Provides
ExecutionContext bindExecutionContext(ScriptSession session) {
return session.getExecutionContext();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import io.deephaven.auth.AuthenticationRequestHandler;
import io.deephaven.configuration.Configuration;
import io.deephaven.engine.context.ExecutionContext;
import io.deephaven.engine.table.impl.perf.QueryPerformanceRecorder;
import io.deephaven.engine.table.impl.perf.UpdatePerformanceTracker;
import io.deephaven.engine.table.impl.util.MemoryTableLoggers;
Expand Down Expand Up @@ -51,6 +52,7 @@ public class DeephavenApiServer {
private final UriResolvers uriResolvers;
private final SessionService sessionService;
private final Map<String, AuthenticationRequestHandler> authenticationHandlers;
private final Provider<ExecutionContext> executionContextProvider;

@Inject
public DeephavenApiServer(
Expand All @@ -62,7 +64,8 @@ public DeephavenApiServer(
final ApplicationInjector applicationInjector,
final UriResolvers uriResolvers,
final SessionService sessionService,
final Map<String, AuthenticationRequestHandler> authenticationHandlers) {
final Map<String, AuthenticationRequestHandler> authenticationHandlers,
final Provider<ExecutionContext> executionContextProvider) {
this.server = server;
this.ugp = ugp;
this.logInit = logInit;
Expand All @@ -72,6 +75,7 @@ public DeephavenApiServer(
this.uriResolvers = uriResolvers;
this.sessionService = sessionService;
this.authenticationHandlers = authenticationHandlers;
this.executionContextProvider = executionContextProvider;
}

@VisibleForTesting
Expand Down Expand Up @@ -162,6 +166,8 @@ public void join() throws InterruptedException {
void startForUnitTests() throws Exception {
pluginRegistration.registerAll();
applicationInjector.run();
executionContextProvider.get().getQueryLibrary().updateVersionString("DEFAULT");

log.info().append("Starting server...").endl();
server.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import dagger.BindsInstance;
import dagger.Component;
import io.deephaven.server.console.SessionToExecutionStateModule;
import io.deephaven.server.console.groovy.GroovyConsoleSessionModule;
import io.deephaven.server.log.LogModule;
import io.grpc.ManagedChannelBuilder;
Expand All @@ -18,7 +19,8 @@
DeephavenApiServerModule.class,
LogModule.class,
GroovyConsoleSessionModule.class,
ServerBuilderInProcessModule.class
ServerBuilderInProcessModule.class,
SessionToExecutionStateModule.class,
})
public interface DeephavenApiServerInProcessGroovyComponent {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import dagger.BindsInstance;
import dagger.Component;
import io.deephaven.server.console.SessionToExecutionStateModule;
import io.deephaven.server.console.python.PythonConsoleSessionModule;
import io.deephaven.server.console.python.PythonGlobalScopeCopyModule;
import io.deephaven.server.log.LogModule;
Expand All @@ -20,7 +21,8 @@
LogModule.class,
PythonConsoleSessionModule.class,
PythonGlobalScopeCopyModule.class,
ServerBuilderInProcessModule.class
ServerBuilderInProcessModule.class,
SessionToExecutionStateModule.class,
})
public interface DeephavenApiServerInProcessPythonComponent {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ public static <T> ExportObject<T> wrapAsExport(final T export) {

@AssistedInject
public SessionState(final Scheduler scheduler,
final Provider<ScriptSession> scriptSessionProvider,
final Provider<ExecutionContext> executionContextProvider,
@Assisted final AuthContext authContext) {
this.sessionId = UuidCreator.toString(UuidCreator.getRandomBased());
this.logPrefix = "SessionState{" + sessionId + "}: ";
this.scheduler = scheduler;
this.authContext = authContext;
this.executionContext = scriptSessionProvider.get().getExecutionContext();
this.executionContext = executionContextProvider.get();
log.info().append(logPrefix).append("session initialized").endl();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package io.deephaven.server.appmode;

import io.deephaven.engine.context.ExecutionContext;
import io.deephaven.engine.liveness.LivenessScopeStack;
import io.deephaven.engine.util.NoLanguageDeephavenSession;
import io.deephaven.engine.util.ScriptSession;
Expand Down Expand Up @@ -42,7 +43,7 @@ public void setup() {
livenessScope = LivenessScopeStack.open();
scheduler = new TestControlledScheduler();
sessionService = new SessionService(scheduler,
authContext -> new SessionState(scheduler, NoLanguageDeephavenSession::new, authContext),
authContext -> new SessionState(scheduler, ExecutionContext::createForUnitTests, authContext),
TOKEN_EXPIRE_MS, Optional.empty(), Collections.emptyMap());
applicationServiceGrpcImpl = new ApplicationServiceGrpcImpl(scheduler, sessionService,
new TypeLookup(ObjectTypeLookup.NoOp.INSTANCE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
*/
package io.deephaven.server.runner;

import dagger.BindsInstance;
import dagger.Component;
import io.deephaven.engine.context.ExecutionContext;
import io.deephaven.engine.updategraph.UpdateGraphProcessor;
import io.deephaven.engine.liveness.LivenessScope;
import io.deephaven.engine.liveness.LivenessScopeStack;
import io.deephaven.io.logger.LogBuffer;
import io.deephaven.io.logger.LogBufferGlobal;
import io.deephaven.proto.DeephavenChannel;
import io.deephaven.server.console.NoConsoleSessionModule;
import io.deephaven.server.log.LogModule;
import io.deephaven.util.SafeCloseable;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
Expand All @@ -17,17 +22,53 @@
import org.junit.Before;
import org.junit.Rule;

import javax.inject.Named;
import javax.inject.Singleton;
import java.io.PrintStream;
import java.util.concurrent.TimeUnit;

/**
* Manages a single instance of {@link DeephavenApiServer}.
*/
public abstract class DeephavenApiServerTestBase {
@Singleton
@Component(modules = {
DeephavenApiServerModule.class,
NoConsoleSessionModule.class,
ServerBuilderInProcessModule.class
})
public interface TestComponent {

DeephavenApiServer getServer();

ManagedChannelBuilder<?> channelBuilder();

@Component.Builder
interface Builder {

@BindsInstance
Builder withSchedulerPoolSize(@Named("scheduler.poolSize") int numThreads);

@BindsInstance
Builder withSessionTokenExpireTmMs(@Named("session.tokenExpireMs") long tokenExpireMs);

@BindsInstance
Builder withOut(@Named("out") PrintStream out);

@BindsInstance
Builder withErr(@Named("err") PrintStream err);

@BindsInstance
Builder withExecutionContext(ExecutionContext context);

TestComponent build();
}
}

@Rule
public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();

private DeephavenApiServerInProcessGroovyComponent serverComponent;
private TestComponent serverComponent;
private LogBuffer logBuffer;
private DeephavenApiServer server;
private SafeCloseable scopeCloseable;
Expand All @@ -40,8 +81,8 @@ public void setUp() throws Exception {
logBuffer = new LogBuffer(128);
LogBufferGlobal.setInstance(logBuffer);

serverComponent = DaggerDeephavenApiServerInProcessGroovyComponent
.builder()
serverComponent = DaggerDeephavenApiServerTestBase_TestComponent.builder()
.withExecutionContext(ExecutionContext.createForUnitTests())
.withSchedulerPoolSize(4)
.withSessionTokenExpireTmMs(sessionTokenExpireTmMs())
.withOut(System.out)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package io.deephaven.server.session;

import io.deephaven.base.verify.Assert;
import io.deephaven.engine.context.ExecutionContext;
import io.deephaven.engine.liveness.LivenessScopeStack;
import io.deephaven.engine.util.NoLanguageDeephavenSession;
import io.deephaven.server.util.TestControlledScheduler;
Expand All @@ -30,7 +31,7 @@ public void setup() {
livenessScope = LivenessScopeStack.open();
scheduler = new TestControlledScheduler();
sessionService = new SessionService(scheduler,
authContext -> new SessionState(scheduler, NoLanguageDeephavenSession::new, authContext),
authContext -> new SessionState(scheduler, ExecutionContext::createForUnitTests, authContext),
TOKEN_EXPIRE_MS, Optional.empty(), Collections.emptyMap());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import io.deephaven.base.verify.Assert;
import io.deephaven.base.verify.AssertionFailure;
import io.deephaven.engine.context.ExecutionContext;
import io.deephaven.engine.util.NoLanguageDeephavenSession;
import io.deephaven.proto.util.ExportTicketHelper;
import io.deephaven.server.table.ExportTableUpdateListenerTest;
Expand Down Expand Up @@ -61,7 +62,7 @@ public void setup() {
livenessScope = new LivenessScope();
LivenessScopeStack.push(livenessScope);
scheduler = new TestControlledScheduler();
session = new SessionState(scheduler, NoLanguageDeephavenSession::new, AUTH_CONTEXT);
session = new SessionState(scheduler, ExecutionContext::createForUnitTests, AUTH_CONTEXT);
session.initializeExpiration(new SessionService.TokenExpiration(UUID.randomUUID(),
DateTimeUtils.nanosToTime(Long.MAX_VALUE), session));
nextExportId = 1;
Expand Down Expand Up @@ -637,7 +638,7 @@ public void testCannotOutOfOrderServerExports() {

@Test
public void testVerifyExpirationSession() {
final SessionState session = new SessionState(scheduler, NoLanguageDeephavenSession::new, AUTH_CONTEXT);
final SessionState session = new SessionState(scheduler, ExecutionContext::createForUnitTests, AUTH_CONTEXT);
final SessionService.TokenExpiration expiration =
new SessionService.TokenExpiration(UUID.randomUUID(), DateTimeUtils.nanosToTime(Long.MAX_VALUE),
session);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package io.deephaven.server.table;

import io.deephaven.base.verify.Assert;
import io.deephaven.engine.context.ExecutionContext;
import io.deephaven.engine.table.impl.TableUpdateImpl;
import io.deephaven.engine.updategraph.UpdateGraphProcessor;
import io.deephaven.engine.util.NoLanguageDeephavenSession;
Expand Down Expand Up @@ -340,7 +341,7 @@ private void expectNoMessage() {

public class TestSessionState extends SessionState {
public TestSessionState() {
super(scheduler, NoLanguageDeephavenSession::new, AUTH_CONTEXT);
super(scheduler, ExecutionContext::createForUnitTests, AUTH_CONTEXT);
initializeExpiration(new SessionService.TokenExpiration(UUID.randomUUID(),
DateTimeUtils.nanosToTime(Long.MAX_VALUE), this));
}
Expand Down

0 comments on commit 92f7c51

Please sign in to comment.