Skip to content

Commit ea17528

Browse files
authored
test: Mock server for testing (#1213)
* This test makes a call to the mock server This will be useful for debugging grpc issues. * Now the test works with the mock server in repo This mock server will be super useful for ensuring that the right data gets transmitted in private preview so that we can confirm the client library is working correctly. * Delete js file We use the ts file instead. * Add a README and guiding steps inline * Simplify the instructions * minor corrections * Resolve linting issues * Add headers for new code * Skip a mock server test
1 parent 09b76ca commit ea17528

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

mock-server/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
The mock server code in this folder is used for observing requests that will
2+
be sent to the server. When the server sends back a vague error it is often
3+
because the request it receives is incorrect so it is crucial to have a tool to
4+
view the request in order to know what client library code needs to change.
5+
6+
The steps to follow for using the mock server is:
7+
1. Take a look at `test/try-server.ts` to see how the mock server is used.
8+
2. In the system test you want to debug, change `it` to `it.only` to isolate that test.
9+
3. In this test, surround the code with `startServer(async () => {`
10+
4. Ensure `apiEndpoint: 'localhost:50051'` is passed into the datastore client that is used for this test.
11+
5. If the system test hits an endpoint other than `runQuery` then in `datastore-server.ts`, change `.addService(service, {runQuery: grpcEndpoint})` to `.addService(service, {<<OtherGrpcEndpoint>>: grpcEndpoint})`
12+
6. Set a breakpoint on the line that says `SET A BREAKPOINT HERE` in `datastore-server.ts`
13+
7. Debug with system tests (Enter `npm run system-test -- --inspect` in terminal)
14+
15+

mock-server/datastore-server.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
const {dirname, resolve} = require('node:path');
16+
17+
const PROTO_PATH = __dirname + '/../protos/google/datastore/v1/datastore.proto';
18+
const DATASTORE_PROTOS = __dirname + '/../protos';
19+
const GAX_PROTOS_DIR = resolve(
20+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
21+
// @ts-ignore
22+
// eslint-disable-next-line n/no-extraneous-require
23+
dirname(require.resolve('google-gax')),
24+
'../protos'
25+
);
26+
27+
const grpc = require('@grpc/grpc-js');
28+
const protoLoader = require('@grpc/proto-loader');
29+
30+
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
31+
keepCase: true,
32+
longs: String,
33+
enums: String,
34+
defaults: true,
35+
oneofs: true,
36+
includeDirs: [DATASTORE_PROTOS, GAX_PROTOS_DIR],
37+
});
38+
const descriptor = grpc.loadPackageDefinition(packageDefinition);
39+
40+
/**
41+
* Implements the runQuery RPC method.
42+
*/
43+
function grpcEndpoint(
44+
call: {},
45+
callback: (arg1: string | null, arg2: {}) => {}
46+
) {
47+
// SET A BREAKPOINT HERE AND EXPLORE `call` TO SEE THE REQUEST.
48+
callback(null, {message: 'Hello'});
49+
}
50+
51+
/**
52+
* Starts an RPC server that receives requests for datastore
53+
*/
54+
export function startServer(cb: () => void) {
55+
const server = new grpc.Server();
56+
const service = descriptor.google.datastore.v1.Datastore.service;
57+
// On the next line, change runQuery to the grpc method you want to investigate
58+
server.addService(service, {runQuery: grpcEndpoint});
59+
server.bindAsync(
60+
'0.0.0.0:50051',
61+
grpc.ServerCredentials.createInsecure(),
62+
() => {
63+
console.log('server started');
64+
cb();
65+
}
66+
);
67+
}

mock-server/package.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "grpc-examples",
3+
"version": "0.1.0",
4+
"dependencies": {
5+
"@grpc/proto-loader": "^0.5.0",
6+
"async": "^1.5.2",
7+
"google-protobuf": "^3.0.0",
8+
"@grpc/grpc-js": "^1.1.0",
9+
"lodash": "^4.6.1",
10+
"minimist": "^1.2.0"
11+
}
12+
}

test/try-server.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import {describe, it} from 'mocha';
16+
import {Datastore} from '../src';
17+
18+
import {startServer} from '../mock-server/datastore-server';
19+
20+
describe('Try server', () => {
21+
it.skip('should try to connect to the running server', done => {
22+
startServer(async () => {
23+
const datastore = new Datastore({
24+
namespace: `${Date.now()}`,
25+
apiEndpoint: 'localhost:50051',
26+
});
27+
const postKey = datastore.key(['Post', 'post1']);
28+
const query = datastore.createQuery('Post').hasAncestor(postKey);
29+
const allResults = await datastore.runQuery(query);
30+
done();
31+
});
32+
});
33+
});

0 commit comments

Comments
 (0)