Skip to content

Commit b14381c

Browse files
author
Paul Korzhyk
committedFeb 12, 2019
Make tests less noisy, maintain preds list
1 parent 1f62110 commit b14381c

File tree

9 files changed

+66
-38
lines changed

9 files changed

+66
-38
lines changed
 

‎lib/clientStub.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var DgraphClientStub = (function () {
3535
if (req.vars != null) {
3636
headers["X-Dgraph-Vars"] = JSON.stringify(req.vars);
3737
}
38-
return this.callAPI("query" + (req.startTs == null ? "" : "/" + req.startTs), {
38+
return this.callAPI("query" + (req.startTs === 0 ? "" : "/" + req.startTs), {
3939
method: "POST",
4040
body: req.query,
4141
headers: headers,
@@ -56,7 +56,7 @@ var DgraphClientStub = (function () {
5656
body = JSON.stringify(obj);
5757
}
5858
else if (mu.setNquads != null || mu.deleteNquads != null) {
59-
body = "{\n " + (mu.setNquads == null ? "" : "set {\n " + mu.setNquads + "\n }") + "\n " + (mu.deleteNquads == null ? "" : "delete {\n " + mu.deleteNquads + "\n }") + "\n }";
59+
body = "{\n " + (mu.setNquads === undefined ? "" : "set {\n " + mu.setNquads + "\n }") + "\n " + (mu.deleteNquads === undefined ? "" : "delete {\n " + mu.deleteNquads + "\n }") + "\n }";
6060
}
6161
else {
6262
return Promise.reject("Mutation mu argument in alter");
@@ -68,7 +68,7 @@ var DgraphClientStub = (function () {
6868
if (mu.commitNow) {
6969
headers["X-Dgraph-CommitNow"] = "true";
7070
}
71-
return this.callAPI("mutate" + (mu.startTs == null ? "" : "/" + mu.startTs), {
71+
return this.callAPI("mutate" + (mu.startTs === 0 ? "" : "/" + mu.startTs), {
7272
method: "POST",
7373
body: body,
7474
headers: headers,

‎lib/txn.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ export declare class Txn {
1313
mutate(mu: Mutation): Promise<Assigned>;
1414
commit(): Promise<void>;
1515
discard(): Promise<void>;
16+
private mergeArrays;
1617
private mergeContext;
1718
}

‎lib/txn.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ var Txn = (function () {
4242
this.finished = false;
4343
this.mutated = false;
4444
this.dc = dc;
45-
this.ctx = { start_ts: 0 };
45+
this.ctx = {
46+
start_ts: 0,
47+
keys: [],
48+
preds: [],
49+
};
4650
}
4751
Txn.prototype.query = function (q) {
4852
return this.queryWithVars(q, null);
@@ -180,8 +184,13 @@ var Txn = (function () {
180184
});
181185
});
182186
};
187+
Txn.prototype.mergeArrays = function (a, b) {
188+
var res = a.slice();
189+
res.push.apply(res, b);
190+
res.sort();
191+
return res.filter(function (item, idx, arr) { return idx === 0 || arr[idx - 1] !== item; });
192+
};
183193
Txn.prototype.mergeContext = function (src) {
184-
var _a;
185194
if (src == null) {
186195
return;
187196
}
@@ -191,13 +200,11 @@ var Txn = (function () {
191200
else if (this.ctx.start_ts !== src.start_ts) {
192201
throw new Error("StartTs mismatch");
193202
}
194-
if (src.keys != null) {
195-
if (this.ctx.keys == null) {
196-
this.ctx.keys = src.keys;
197-
}
198-
else {
199-
(_a = this.ctx.keys).push.apply(_a, src.keys);
200-
}
203+
if (src.keys !== null) {
204+
this.ctx.keys = this.mergeArrays(this.ctx.keys, src.keys);
205+
}
206+
if (src.preds !== null) {
207+
this.ctx.preds = this.mergeArrays(this.ctx.preds, src.preds);
201208
}
202209
};
203210
return Txn;

‎lib/types.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface Request {
1111
vars?: {
1212
[k: string]: string;
1313
} | null;
14-
startTs?: number | null;
14+
startTs?: number;
1515
}
1616
export interface Response {
1717
data: {};
@@ -22,7 +22,7 @@ export interface Mutation {
2222
deleteJson?: object | null;
2323
setNquads?: string | null;
2424
deleteNquads?: string | null;
25-
startTs?: number | null;
25+
startTs?: number;
2626
commitNow?: boolean | null;
2727
}
2828
export interface Assigned {
@@ -40,9 +40,9 @@ export interface Extensions {
4040
}
4141
export interface TxnContext {
4242
start_ts: number;
43-
commit_ts?: number | null;
4443
aborted?: boolean | null;
4544
keys?: string[] | null;
45+
preds?: string[] | null;
4646
}
4747
export interface Latency {
4848
parsing_ns?: number | null;

‎src/clientStub.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class DgraphClientStub {
5050
headers["X-Dgraph-Vars"] = JSON.stringify(req.vars);
5151
}
5252

53-
return this.callAPI(`query${req.startTs == null ? "" : `/${req.startTs}`}`, {
53+
return this.callAPI(`query${req.startTs === 0 ? "" : `/${req.startTs}`}`, {
5454
method: "POST",
5555
body: req.query,
5656
headers,
@@ -73,10 +73,10 @@ export class DgraphClientStub {
7373
body = JSON.stringify(obj);
7474
} else if (mu.setNquads != null || mu.deleteNquads != null) {
7575
body = `{
76-
${mu.setNquads == null ? "" : `set {
76+
${mu.setNquads === undefined ? "" : `set {
7777
${mu.setNquads}
7878
}`}
79-
${mu.deleteNquads == null ? "" : `delete {
79+
${mu.deleteNquads === undefined ? "" : `delete {
8080
${mu.deleteNquads}
8181
}`}
8282
}`;
@@ -92,7 +92,7 @@ export class DgraphClientStub {
9292
headers["X-Dgraph-CommitNow"] = "true";
9393
}
9494

95-
return this.callAPI(`mutate${mu.startTs == null ? "" : `/${mu.startTs}`}`, {
95+
return this.callAPI(`mutate${mu.startTs === 0 ? "" : `/${mu.startTs}`}`, {
9696
method: "POST",
9797
body,
9898
headers,

‎src/txn.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export class Txn {
2929

3030
constructor(dc: DgraphClient) {
3131
this.dc = dc;
32-
this.ctx = { start_ts: 0 };
32+
this.ctx = {
33+
start_ts: 0,
34+
keys: [],
35+
preds: [],
36+
};
3337
}
3438

3539
/**
@@ -184,6 +188,15 @@ export class Txn {
184188
await c.abort(this.ctx);
185189
}
186190

191+
private mergeArrays(a: string[], b: string[]) {
192+
const res = a.slice();
193+
res.push(...b);
194+
res.sort();
195+
// Filter unique in a sorted array.
196+
return res.filter(
197+
(item: string, idx: number, arr: string[]) => idx === 0 || arr[idx - 1] !== item);
198+
}
199+
187200
private mergeContext(src?: TxnContext | null): void {
188201
if (src == null) {
189202
// This condition will be true only if the server doesn't return a txn context after a query or mutation.
@@ -197,12 +210,11 @@ export class Txn {
197210
throw new Error("StartTs mismatch");
198211
}
199212

200-
if (src.keys != null) {
201-
if (this.ctx.keys == null) {
202-
this.ctx.keys = src.keys;
203-
} else {
204-
this.ctx.keys.push(...src.keys);
205-
}
213+
if (src.keys !== null) {
214+
this.ctx.keys = this.mergeArrays(this.ctx.keys, src.keys);
215+
}
216+
if (src.preds !== null) {
217+
this.ctx.preds = this.mergeArrays(this.ctx.preds, src.preds);
206218
}
207219
}
208220
}

‎src/types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface Payload {
1111
export interface Request {
1212
query: string;
1313
vars?: { [k: string]: string } | null;
14-
startTs?: number | null;
14+
startTs?: number;
1515
}
1616

1717
export interface Response {
@@ -24,7 +24,7 @@ export interface Mutation {
2424
deleteJson?: object | null;
2525
setNquads?: string | null;
2626
deleteNquads?: string | null;
27-
startTs?: number | null;
27+
startTs?: number;
2828
commitNow?: boolean | null;
2929
}
3030

@@ -44,9 +44,9 @@ export interface Extensions {
4444

4545
export interface TxnContext {
4646
start_ts: number;
47-
commit_ts?: number | null;
4847
aborted?: boolean | null;
4948
keys?: string[] | null;
49+
preds?: string[] | null;
5050
}
5151

5252
export interface Latency {

‎tests/integration/acctUpsert.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async function tryUpsert(account: Account): Promise<void> {
6767
}
6868
}
6969

70-
let startStatus = 0; // set at the start of doUpserts
70+
let startTime = 0; // set at the start of doUpserts
7171
let lastStatus = 0;
7272
let cancelled = false; // cancelled due to timeout
7373

@@ -76,9 +76,9 @@ let retryCount = 0;
7676

7777
function conditionalLog(): void {
7878
const now = new Date().getTime();
79-
if (now - lastStatus > 1000 && !cancelled) {
79+
if (now - lastStatus > 4000 && !cancelled) {
8080
// tslint:disable-next-line no-console
81-
console.log(`Success: ${successCount}, Retries: ${retryCount}, Total Time: ${now - startStatus} ms`);
81+
console.log(`Success: ${successCount}, Retries: ${retryCount}, Total Time: ${now - startTime} ms`);
8282
lastStatus = now;
8383
}
8484
}
@@ -111,7 +111,7 @@ async function doUpserts(): Promise<void> {
111111
}
112112
}
113113

114-
startStatus = new Date().getTime();
114+
startTime = new Date().getTime();
115115
const id = setTimeout(
116116
() => {
117117
cancelled = true;

‎tests/integration/bank.spec.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,23 @@ async function createAccounts(): Promise<void> {
3737
});
3838
}
3939

40-
let startStatus = 0; // set before Promise.all
40+
let startTime = 0; // set before Promise.all
41+
let lastStatus = new Date().getTime();
4142
let cancelled = false;
4243
let finished = false;
4344

4445
let runs = 0;
4546
let aborts = 0;
4647

48+
function conditionalLog(): void {
49+
const now = new Date().getTime();
50+
if (now - lastStatus > 4000 && !cancelled) {
51+
// tslint:disable-next-line no-console
52+
console.log(`Runs: ${runs}, Aborts: ${aborts}, Total Time: ${new Date().getTime() - startTime} ms`);
53+
lastStatus = now;
54+
}
55+
}
56+
4757
async function runTotal(): Promise<void> {
4858
const res = await client.newTxn().query(`{
4959
var(func: uid(${uids.join(",")})) {
@@ -55,9 +65,7 @@ async function runTotal(): Promise<void> {
5565
}`);
5666
// tslint:disable-next-line no-unsafe-any
5767
expect((<{ total: { bal: number }[] }>res.data).total[0].bal).toBe(uids.length * initialBalance);
58-
59-
// tslint:disable-next-line no-console
60-
console.log(`Runs: ${runs}, Aborts: ${aborts}, Total Time: ${new Date().getTime() - startStatus} ms`);
68+
conditionalLog();
6169
}
6270

6371
async function runTotalInLoop(): Promise<void> {
@@ -129,7 +137,7 @@ describe("bank", () => {
129137
promises.push(runTxnInLoop());
130138
}
131139

132-
startStatus = new Date().getTime();
140+
startTime = new Date().getTime();
133141
const id = setTimeout(
134142
() => {
135143
cancelled = true;

0 commit comments

Comments
 (0)
Please sign in to comment.