blob: 57f520f8acd64ed2d1dd79e6567cbc389b2a23cf [file] [log] [blame]
Mustafa Senol Cosar3f0d4442019-03-01 18:57:09 +03001#!/usr/bin/env node
2
3/*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22const assert = require("assert");
23const test = require("tape");
24const thrift = require("thrift");
Cameron Martinfdaca5e2025-01-07 15:25:15 +000025const { program } = require("commander");
CJCombrink40d3e782026-03-23 06:09:04 +000026const tape = require("tape");
Mustafa Senol Cosar3f0d4442019-03-01 18:57:09 +030027
28program
29 .option("--host <host>", "Set the thrift server host to connect", "localhost")
30 .option("--port <port>", "Set the thrift server port number to connect", 9090)
CJCombrink40d3e782026-03-23 06:09:04 +000031 .option("--base <base>", "Set the base: 'pure', 'base' or 'extend'", "pure")
Mustafa Senol Cosar3f0d4442019-03-01 18:57:09 +030032 .parse(process.argv);
33
CJCombrink40d3e782026-03-23 06:09:04 +000034const ServiceBase = require("types-package/first-episode/BaseService");
35const ServicePure = require("./gen-2/second-episode/gen-nodejs/Service");
36const ServiceExtended = require("./gen-2/second-episode/gen-nodejs/ExtendedService");
Mustafa Senol Cosar3f0d4442019-03-01 18:57:09 +030037const Types = require("types-package/first-episode/Types_types");
38
Cameron Martinfdaca5e2025-01-07 15:25:15 +000039const opts = program.opts();
40const host = opts.host;
41const port = opts.port;
Mustafa Senol Cosar3f0d4442019-03-01 18:57:09 +030042
CJCombrink40d3e782026-03-23 06:09:04 +000043let Service;
44if (opts.base === "pure") {
45 Service = ServicePure;
46} else if (opts.base === "base") {
47 Service = ServiceBase;
48} else if (opts.base === "extend") {
49 Service = ServiceExtended;
50}
51
Mustafa Senol Cosar3f0d4442019-03-01 18:57:09 +030052const options = {
53 transport: thrift.TBufferedTransport,
Cameron Martincaef0ed2025-01-15 11:58:39 +010054 protocol: thrift.TJSONProtocol,
Mustafa Senol Cosar3f0d4442019-03-01 18:57:09 +030055};
56
57const connection = thrift.createConnection(host, port, options);
Cameron Martincaef0ed2025-01-15 11:58:39 +010058const testDriver = function (client, callback) {
59 test("NodeJS episodic compilation client-server test", function (assert) {
Mustafa Senol Cosar3f0d4442019-03-01 18:57:09 +030060 const type1Object = new Types.Type1();
61 type1Object.number = 42;
62 type1Object.message = "The answer";
Cameron Martincaef0ed2025-01-15 11:58:39 +010063 client.testEpisode(type1Object, function (err, response) {
Mustafa Senol Cosar3f0d4442019-03-01 18:57:09 +030064 assert.error(err, "no callback error");
65 assert.equal(response.number, type1Object.number + 1);
66 assert.equal(
67 response.message,
Cameron Martincaef0ed2025-01-15 11:58:39 +010068 type1Object.message + " [Hello from the server]",
Mustafa Senol Cosar3f0d4442019-03-01 18:57:09 +030069 );
70 assert.end();
71 callback("Server successfully tested");
72 });
73 });
CJCombrink40d3e782026-03-23 06:09:04 +000074
75 if (opts.base === "extend") {
76 test("NodeJS episodic compilation client-server extended test", function (assert) {
77 const type1Object = new Types.Type1();
78 type1Object.number = 42;
79 type1Object.message = "The answer";
80 client.testEpisodeExtend(type1Object, function (err, response) {
81 assert.error(err, "no callback error");
82 assert.equal(response.number, type1Object.number + 1);
83 assert.equal(
84 response.message,
85 type1Object.message + " [Hello from the extended server]",
86 );
87 assert.end();
88 callback("Extended Server successfully tested");
89 });
90 });
91 }
Mustafa Senol Cosar3f0d4442019-03-01 18:57:09 +030092};
93
Cameron Martincaef0ed2025-01-15 11:58:39 +010094connection.on("error", function (err) {
Mustafa Senol Cosar3f0d4442019-03-01 18:57:09 +030095 assert(false, err);
96});
97
98const client = thrift.createClient(Service, connection);
99
100runTests();
101
102function runTests() {
Cameron Martincaef0ed2025-01-15 11:58:39 +0100103 testDriver(client, function (status) {
Mustafa Senol Cosar3f0d4442019-03-01 18:57:09 +0300104 console.log(status);
CJCombrink40d3e782026-03-23 06:09:04 +0000105 });
106 tape.onFinish(function () {
107 console.log("Tests finished");
Mustafa Senol Cosar3f0d4442019-03-01 18:57:09 +0300108 connection.destroy();
109 });
110}
111
Cameron Martincaef0ed2025-01-15 11:58:39 +0100112exports.expressoTest = function () {};