@lightbend/akkaserverless-javascript-sdk
Version:
Akka Serverless JavaScript SDK
116 lines • 4.86 kB
JavaScript
;
/*
* Copyright 2021 Lightbend Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.IntegrationTestkit = void 0;
const tslib_1 = require("tslib");
const grpc = tslib_1.__importStar(require("@grpc/grpc-js"));
const settings = tslib_1.__importStar(require("../settings"));
const grpc_util_1 = require("./grpc-util");
const akkaserverless_1 = require("./akkaserverless");
const testcontainers_1 = require("testcontainers");
const defaultDockerImage = `gcr.io/akkaserverless-public/akkaserverless-proxy:${settings.frameworkVersion}`;
/**
* Integration Testkit.
*/
class IntegrationTestkit {
constructor(options) {
this.options = { dockerImage: defaultDockerImage };
if (options) {
this.options = options;
if (!options.dockerImage) {
this.options.dockerImage = defaultDockerImage;
}
}
this.clients = {};
this.akkaServerless = new akkaserverless_1.AkkaServerless(options);
}
/**
* Add the given component to this testkit.
*
* @param component - the component to add
* @returns this testkit
*/
addComponent(component) {
this.akkaServerless.addComponent(component);
return this;
}
// This encoding is compatible with this issue:
// https://github.com/mochajs/mocha/issues/2407
/**
* Start the testkit, with the registered components.
*
* @param callback - start callback, accepting possible error
*/
start(callback) {
const result = this.asyncStart();
if (typeof callback === 'function') {
result.then(() => callback(), (error) => callback(error));
}
}
asyncStart() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
// First start this user function
const boundPort = yield this.akkaServerless.start({ port: 0 });
yield testcontainers_1.TestContainers.exposeHostPorts(boundPort);
const proxyContainer = yield new testcontainers_1.GenericContainer(this.options.dockerImage)
.withExposedPorts(9000)
.withEnv('USER_FUNCTION_HOST', 'host.testcontainers.internal')
.withEnv('USER_FUNCTION_PORT', boundPort.toString())
.withEnv('HTTP_PORT', '9000')
.withEnv('VERSION_CHECK_ON_STARTUP', process.env.VERSION_CHECK_ON_STARTUP || 'true')
.withWaitStrategy(testcontainers_1.Wait.forLogMessage('gRPC proxy started'))
.start();
this.proxyContainer = proxyContainer;
const proxyPort = proxyContainer.getMappedPort(9000);
// Create clients
this.akkaServerless.getComponents().forEach((entity) => {
const parts = entity.serviceName ? entity.serviceName.split('.') : [];
if (entity.grpc) {
let stub = entity.grpc;
parts.forEach((part) => {
stub = stub[part];
});
const client = new stub('localhost:' + proxyPort, grpc.credentials.createInsecure());
this.clients[parts[parts.length - 1]] = grpc_util_1.GrpcUtil.promisifyClient(client, 'Async');
}
});
});
}
/**
* Shut down the testkit.
*
* @param callback - shutdown callback, accepting possible error
*/
shutdown(callback) {
Object.getOwnPropertyNames(this.clients).forEach((client) => {
this.clients[client].close();
});
let proxyContainerStopped;
if (this.proxyContainer !== undefined) {
// Important, ensure that the proxy container is stopped before we shut down
// ourselves, otherwise it will try to reconnect and that will cause unhandled
// exceptions.
proxyContainerStopped = this.proxyContainer.stop();
}
else {
proxyContainerStopped = Promise.resolve();
}
proxyContainerStopped.then(() => this.akkaServerless.tryShutdown(callback), (err) => this.akkaServerless.tryShutdown(() => callback(err)));
}
}
exports.IntegrationTestkit = IntegrationTestkit;
//# sourceMappingURL=integration-testkit.js.map