Skip to content

Instantly share code, notes, and snippets.

View rowanl's full-sized avatar

Rowan rowanl

View GitHub Profile
private <T> CompletionStage<T> wrapContext(Supplier<T> toRun) {
return supplyAsync(toRun, executionContext); // here we are passing the context to run the method in, this means this is running in our new actor context
}
// and here is the magic, we are returning async and we are doing it in the context of executionContext. For the JS folk this is like using call or apply, we are saying do this call, but do this call within a different context than the http thread you were called from
public CompletionStage<List<Charity>> findList() {
return wrapContext(() -> ebeanServer.find(Charity.class).findList());
}
@rowanl
rowanl / CharityRepository.java
Created January 17, 2018 20:33
playframework-kubernetes - Avoiding bug with Ebean and DI
@SuppressWarnings("unused") EbeanDynamicEvolutions ebeanDynamicEvolutions /* Required for Ebean/DI bug */) {
@rowanl
rowanl / Dockerfile
Created January 17, 2018 20:30
playframework-kubernetes - Package and debug commands using sbt with Docker
EXPOSE 9000 5005
RUN unzip target/universal/$APP_NAME-$APP_VERSION.zip
RUN chmod +x $APP_NAME-$APP_VERSION/bin/$APP_NAME
ENTRYPOINT $APP_NAME-$APP_VERSION/bin/$APP_NAME -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
@rowanl
rowanl / Dockerfile
Created January 17, 2018 20:25
playframework-kubernetes - Package and run commands using sbt with Docker
RUN sbt test dist
EXPOSE 9000
RUN unzip target/universal/$APP_NAME-$APP_VERSION.zip
RUN chmod +x $APP_NAME-$APP_VERSION/bin/$APP_NAME
ENTRYPOINT $APP_NAME-$APP_VERSION/bin/$APP_NAME
@rowanl
rowanl / build.sbt
Created January 17, 2018 20:20
playframework-kubernetes - Use different conf file for tests
javaOptions in Test ++= Seq("-Dconfig.file=conf/application.test.conf")
@rowanl
rowanl / CharityRepository.java
Last active January 17, 2018 22:04
playframework-kubernetes - Asynchronous, non-blocking implementation of find all query using Ebean
private final EbeanServer ebeanServer;
private final DatabaseExecutionContext executionContext;
@Inject
public CharityRepository(EbeanConfig ebeanConfig, DatabaseExecutionContext executionContext,
@SuppressWarnings("unused")
EbeanDynamicEvolutions ebeanDynamicEvolutions /* Required for Ebean/DI bug */) {
this.ebeanServer = getServer(ebeanConfig.defaultServer());
this.executionContext = executionContext;
}
@rowanl
rowanl / CharityRepository.java
Created January 17, 2018 20:15
playframework-kubernetes - Empty implementation of find all method
public CompletionStage<List<Charity>> findList() {
return null;
}
@rowanl
rowanl / CharityRepositoryTest.java
Created January 17, 2018 20:14
playframework-kubernetes - Testing Ebean queries using BDD
@Test
public void Given_CharityObjectsExist_When_FindListIsCalled_Then_AllCharityObjectsAreReturned() {
// GIVEN
rangeClosed(1, 4).boxed().map(Charity::new).forEach(Model::save);
// WHEN
List<Charity> result = repository.findList().toCompletableFuture().join();
// THEN
@rowanl
rowanl / CharityRepositoryTest.java
Last active January 17, 2018 20:12
playframework-kubernetes - Testing a repository that uses Ebean
public class CharityRepositoryTest extends WithApplication {
private CharityRepository repository;
@Before
public void setUp() {
repository = instanceOf(CharityRepository.class);
CallableSql cleanCharityTable = getDefaultServer().createCallableSql("delete from charity");
getDefaultServer().execute(cleanCharityTable);
@rowanl
rowanl / CharityControllerTest.java
Created January 17, 2018 20:06
playframework-kubernetes - Functional test with mocked time dependency
@Test
public void Given_IsBeforeEndDate_When_GetRequestIsMadeToIndexPage_Then_StatusIs200() {
// GIVEN
long beforeEndDate = 9L;
given(clock.instant()).willReturn(ofEpochMilli(beforeEndDate));
given(clock.getZone()).willReturn(ZoneId.of("+0"));
// WHEN
WSResponse result = newClient(providePort()).url("/").get().toCompletableFuture().join();