Skip to content

Commit bc68e9e

Browse files
committed
Fix build and simplify clientStub tests
1 parent 06d9062 commit bc68e9e

File tree

9 files changed

+65
-47
lines changed

9 files changed

+65
-47
lines changed

lib/client.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as grpc from "grpc";
12
import * as messages from "../generated/api_pb";
23
import { DgraphClientStub } from "./clientStub";
34
import { Txn } from "./txn";
@@ -7,7 +8,7 @@ export declare class DgraphClient {
78
private linRead;
89
private debugMode;
910
constructor(...clients: DgraphClientStub[]);
10-
alter(op: messages.Operation): Promise<types.Payload>;
11+
alter(op: messages.Operation, options?: grpc.CallOptions | null): Promise<types.Payload>;
1112
newTxn(): Txn;
1213
setDebugMode(mode?: boolean): void;
1314
debug(msg: string): void;

lib/client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var DgraphClient = (function () {
5353
this.clients = clients;
5454
this.linRead = new messages.LinRead();
5555
}
56-
DgraphClient.prototype.alter = function (op) {
56+
DgraphClient.prototype.alter = function (op, options) {
5757
return __awaiter(this, void 0, void 0, function () {
5858
var c, pl, _a, _b;
5959
return __generator(this, function (_c) {
@@ -62,7 +62,7 @@ var DgraphClient = (function () {
6262
this.debug("Alter request:\n" + util_1.stringifyMessage(op));
6363
c = this.anyClient();
6464
_b = (_a = types).createPayload;
65-
return [4, c.alter(op)];
65+
return [4, c.alter(op, options)];
6666
case 1:
6767
pl = _b.apply(_a, [_c.sent()]);
6868
this.debug("Alter response:\n" + util_1.stringifyMessage(pl));

lib/clientStub.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import * as messages from "../generated/api_pb";
33
export declare class DgraphClientStub {
44
private stub;
55
private promisified;
6-
constructor(addr?: string | null, credentials?: grpc.ChannelCredentials | null);
7-
alter(op: messages.Operation): Promise<messages.Payload>;
8-
query(req: messages.Request): Promise<messages.Response>;
9-
mutate(mu: messages.Mutation): Promise<messages.Assigned>;
10-
commitOrAbort(ctx: messages.TxnContext): Promise<messages.TxnContext>;
11-
checkVersion(check: messages.Check): Promise<messages.Version>;
6+
constructor(addr?: string | null, credentials?: grpc.ChannelCredentials | null, options?: object | null);
7+
alter(op: messages.Operation, options?: grpc.CallOptions | null): Promise<messages.Payload>;
8+
query(req: messages.Request, options?: grpc.CallOptions | null): Promise<messages.Response>;
9+
mutate(mu: messages.Mutation, options?: grpc.CallOptions | null): Promise<messages.Assigned>;
10+
commitOrAbort(ctx: messages.TxnContext, options?: grpc.CallOptions | null): Promise<messages.TxnContext>;
11+
checkVersion(check: messages.Check, options?: grpc.CallOptions | null): Promise<messages.Version>;
1212
waitForReady(deadline: grpc.Deadline): Promise<void>;
1313
close(): void;
1414
grpcClient(): grpc.Client;

lib/clientStub.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,37 @@ var grpc = require("grpc");
44
var services = require("../generated/api_grpc_pb");
55
var util_1 = require("./util");
66
var DgraphClientStub = (function () {
7-
function DgraphClientStub(addr, credentials) {
7+
function DgraphClientStub(addr, credentials, options) {
88
if (addr == null) {
99
addr = "localhost:9080";
1010
}
1111
if (credentials == null) {
1212
credentials = grpc.credentials.createInsecure();
1313
}
14-
this.stub = new services.DgraphClient(addr, credentials);
14+
this.stub = new services.DgraphClient(addr, credentials, options);
1515
this.promisified = {
16-
alter: util_1.promisify(this.stub.alter, this.stub),
17-
query: util_1.promisify(this.stub.query, this.stub),
18-
mutate: util_1.promisify(this.stub.mutate, this.stub),
19-
commitOrAbort: util_1.promisify(this.stub.commitOrAbort, this.stub),
20-
checkVersion: util_1.promisify(this.stub.checkVersion, this.stub),
21-
waitForReady: util_1.promisify(this.stub.waitForReady, this.stub),
16+
alter: util_1.promisify3(this.stub.alter, this.stub),
17+
query: util_1.promisify3(this.stub.query, this.stub),
18+
mutate: util_1.promisify3(this.stub.mutate, this.stub),
19+
commitOrAbort: util_1.promisify3(this.stub.commitOrAbort, this.stub),
20+
checkVersion: util_1.promisify3(this.stub.checkVersion, this.stub),
21+
waitForReady: util_1.promisify1(this.stub.waitForReady, this.stub),
2222
};
2323
}
24-
DgraphClientStub.prototype.alter = function (op) {
25-
return this.promisified.alter(op);
24+
DgraphClientStub.prototype.alter = function (op, options) {
25+
return this.promisified.alter(op, undefined, options);
2626
};
27-
DgraphClientStub.prototype.query = function (req) {
28-
return this.promisified.query(req);
27+
DgraphClientStub.prototype.query = function (req, options) {
28+
return this.promisified.query(req, undefined, options);
2929
};
30-
DgraphClientStub.prototype.mutate = function (mu) {
31-
return this.promisified.mutate(mu);
30+
DgraphClientStub.prototype.mutate = function (mu, options) {
31+
return this.promisified.mutate(mu, undefined, options);
3232
};
33-
DgraphClientStub.prototype.commitOrAbort = function (ctx) {
34-
return this.promisified.commitOrAbort(ctx);
33+
DgraphClientStub.prototype.commitOrAbort = function (ctx, options) {
34+
return this.promisified.commitOrAbort(ctx, undefined, options);
3535
};
36-
DgraphClientStub.prototype.checkVersion = function (check) {
37-
return this.promisified.checkVersion(check);
36+
DgraphClientStub.prototype.checkVersion = function (check, options) {
37+
return this.promisified.checkVersion(check, undefined, options);
3838
};
3939
DgraphClientStub.prototype.waitForReady = function (deadline) {
4040
return this.promisified.waitForReady(deadline);

lib/txn.d.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as grpc from "grpc";
12
import * as messages from "../generated/api_pb";
23
import { DgraphClient } from "./client";
34
import * as types from "./types";
@@ -7,12 +8,12 @@ export declare class Txn {
78
private finished;
89
private mutated;
910
constructor(dc: DgraphClient);
10-
query(q: string): Promise<types.Response>;
11+
query(q: string, options?: grpc.CallOptions | null): Promise<types.Response>;
1112
queryWithVars(q: string, vars?: {
1213
[k: string]: any;
13-
} | null): Promise<types.Response>;
14-
mutate(mu: types.Mutation): Promise<messages.Assigned>;
15-
commit(): Promise<void>;
16-
discard(): Promise<void>;
14+
} | null, options?: grpc.CallOptions | null): Promise<types.Response>;
15+
mutate(mu: types.Mutation, options?: grpc.CallOptions | null): Promise<messages.Assigned>;
16+
commit(options?: grpc.CallOptions | null): Promise<void>;
17+
discard(options?: grpc.CallOptions | null): Promise<void>;
1718
private mergeContext(src?);
1819
}

lib/txn.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ var Txn = (function () {
4747
this.ctx = new messages.TxnContext();
4848
this.ctx.setLinRead(this.dc.getLinRead());
4949
}
50-
Txn.prototype.query = function (q) {
51-
return this.queryWithVars(q);
50+
Txn.prototype.query = function (q, options) {
51+
return this.queryWithVars(q, null, options);
5252
};
53-
Txn.prototype.queryWithVars = function (q, vars) {
53+
Txn.prototype.queryWithVars = function (q, vars, options) {
5454
return __awaiter(this, void 0, void 0, function () {
5555
var req, varsMap_1, c, res, _a, _b;
5656
return __generator(this, function (_c) {
@@ -76,7 +76,7 @@ var Txn = (function () {
7676
this.dc.debug("Query request:\n" + util_1.stringifyMessage(req));
7777
c = this.dc.anyClient();
7878
_b = (_a = types).createResponse;
79-
return [4, c.query(req)];
79+
return [4, c.query(req, options)];
8080
case 1:
8181
res = _b.apply(_a, [_c.sent()]);
8282
this.mergeContext(res.getTxn());
@@ -86,7 +86,7 @@ var Txn = (function () {
8686
});
8787
});
8888
};
89-
Txn.prototype.mutate = function (mu) {
89+
Txn.prototype.mutate = function (mu, options) {
9090
return __awaiter(this, void 0, void 0, function () {
9191
var ag, c, e_1, e_2;
9292
return __generator(this, function (_a) {
@@ -103,7 +103,7 @@ var Txn = (function () {
103103
_a.label = 1;
104104
case 1:
105105
_a.trys.push([1, 3, , 8]);
106-
return [4, c.mutate(mu)];
106+
return [4, c.mutate(mu, options)];
107107
case 2:
108108
ag = _a.sent();
109109
return [3, 8];
@@ -131,7 +131,7 @@ var Txn = (function () {
131131
});
132132
});
133133
};
134-
Txn.prototype.commit = function () {
134+
Txn.prototype.commit = function (options) {
135135
return __awaiter(this, void 0, void 0, function () {
136136
var c, e_3;
137137
return __generator(this, function (_a) {
@@ -148,7 +148,7 @@ var Txn = (function () {
148148
_a.label = 1;
149149
case 1:
150150
_a.trys.push([1, 3, , 4]);
151-
return [4, c.commitOrAbort(this.ctx)];
151+
return [4, c.commitOrAbort(this.ctx, options)];
152152
case 2:
153153
_a.sent();
154154
return [3, 4];
@@ -160,7 +160,7 @@ var Txn = (function () {
160160
});
161161
});
162162
};
163-
Txn.prototype.discard = function () {
163+
Txn.prototype.discard = function (options) {
164164
return __awaiter(this, void 0, void 0, function () {
165165
var c;
166166
return __generator(this, function (_a) {
@@ -175,7 +175,7 @@ var Txn = (function () {
175175
}
176176
this.ctx.setAborted(true);
177177
c = this.dc.anyClient();
178-
return [4, c.commitOrAbort(this.ctx)];
178+
return [4, c.commitOrAbort(this.ctx, options)];
179179
case 1:
180180
_a.sent();
181181
return [2];

lib/util.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export declare function errorCode(err: any): {
77
};
88
export declare function isAbortedError(err: any): boolean;
99
export declare function isConflictError(err: any): boolean;
10-
export declare function promisify<A, T>(f: (arg: A, cb: (err?: Error | null, res?: T) => void) => void, thisContext?: any): (arg: A) => Promise<T>;
10+
export declare function promisify1<A, T>(f: (arg: A, cb: (err?: Error | null, res?: T) => void) => void, thisContext?: any): (arg: A) => Promise<T>;
11+
export declare function promisify3<A, B, C, T>(f: (argA: A, argB: B, argC: C, cb: (err?: Error | null, res?: T) => void) => void, thisContext?: any): (argA: A, argB: B, argC: C) => Promise<T>;
1112
export declare function stringifyMessage(msg: jspb.Message): string;
1213
export declare function isBase64(str: string): boolean;
1314
export declare function strToB64(str: string): string;

lib/util.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,22 @@ function isConflictError(err) {
4242
return ec.valid && (ec.code === grpc.status.ABORTED || ec.code === grpc.status.FAILED_PRECONDITION);
4343
}
4444
exports.isConflictError = isConflictError;
45-
function promisify(f, thisContext) {
45+
function promisify1(f, thisContext) {
4646
return function (arg) {
4747
return new Promise(function (resolve, reject) {
4848
f.call(thisContext, arg, function (err, result) { return (err != null) ? reject(err) : resolve(result); });
4949
});
5050
};
5151
}
52-
exports.promisify = promisify;
52+
exports.promisify1 = promisify1;
53+
function promisify3(f, thisContext) {
54+
return function (argA, argB, argC) {
55+
return new Promise(function (resolve, reject) {
56+
f.call(thisContext, argA, argB, argC, function (err, result) { return (err != null) ? reject(err) : resolve(result); });
57+
});
58+
};
59+
}
60+
exports.promisify3 = promisify3;
5361
function stringifyMessage(msg) {
5462
return JSON.stringify(msg.toObject());
5563
}

tests/clientStub.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ describe("clientStub", () => {
2424
const client = await setup();
2525
await checkVersion(client.anyClient());
2626
});
27-
});
2827

29-
describe("checkVersion with call options", () => {
3028
it("should check version with call options", async () => {
3129
const clientStub = new dgraph.DgraphClientStub(SERVER_ADDR, SERVER_CREDENTIALS);
3230
const p = clientStub.checkVersion(new dgraph.Check(), {
@@ -55,4 +53,13 @@ describe("clientStub", () => {
5553
await expect(p).rejects.toBeDefined();
5654
});
5755
});
56+
57+
describe("grpcClient", () => {
58+
it("should close channel if grpc client is closed", async () => {
59+
const clientStub = new dgraph.DgraphClientStub(SERVER_ADDR, SERVER_CREDENTIALS);
60+
clientStub.grpcClient().close();
61+
const p = clientStub.checkVersion(new dgraph.Check());
62+
await expect(p).rejects.toBeDefined();
63+
});
64+
});
5865
});

0 commit comments

Comments
 (0)