Skip to content

Commit

Permalink
Make tests less noisy, maintain preds list
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Korzhyk committed Feb 12, 2019
1 parent 1f62110 commit b14381c
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 38 deletions.
6 changes: 3 additions & 3 deletions lib/clientStub.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var DgraphClientStub = (function () {
if (req.vars != null) {
headers["X-Dgraph-Vars"] = JSON.stringify(req.vars);
}
return this.callAPI("query" + (req.startTs == null ? "" : "/" + req.startTs), {
return this.callAPI("query" + (req.startTs === 0 ? "" : "/" + req.startTs), {
method: "POST",
body: req.query,
headers: headers,
Expand All @@ -56,7 +56,7 @@ var DgraphClientStub = (function () {
body = JSON.stringify(obj);
}
else if (mu.setNquads != null || mu.deleteNquads != null) {
body = "{\n " + (mu.setNquads == null ? "" : "set {\n " + mu.setNquads + "\n }") + "\n " + (mu.deleteNquads == null ? "" : "delete {\n " + mu.deleteNquads + "\n }") + "\n }";
body = "{\n " + (mu.setNquads === undefined ? "" : "set {\n " + mu.setNquads + "\n }") + "\n " + (mu.deleteNquads === undefined ? "" : "delete {\n " + mu.deleteNquads + "\n }") + "\n }";
}
else {
return Promise.reject("Mutation mu argument in alter");
Expand All @@ -68,7 +68,7 @@ var DgraphClientStub = (function () {
if (mu.commitNow) {
headers["X-Dgraph-CommitNow"] = "true";
}
return this.callAPI("mutate" + (mu.startTs == null ? "" : "/" + mu.startTs), {
return this.callAPI("mutate" + (mu.startTs === 0 ? "" : "/" + mu.startTs), {
method: "POST",
body: body,
headers: headers,
Expand Down
1 change: 1 addition & 0 deletions lib/txn.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export declare class Txn {
mutate(mu: Mutation): Promise<Assigned>;
commit(): Promise<void>;
discard(): Promise<void>;
private mergeArrays;
private mergeContext;
}
25 changes: 16 additions & 9 deletions lib/txn.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ var Txn = (function () {
this.finished = false;
this.mutated = false;
this.dc = dc;
this.ctx = { start_ts: 0 };
this.ctx = {
start_ts: 0,
keys: [],
preds: [],
};
}
Txn.prototype.query = function (q) {
return this.queryWithVars(q, null);
Expand Down Expand Up @@ -180,8 +184,13 @@ var Txn = (function () {
});
});
};
Txn.prototype.mergeArrays = function (a, b) {
var res = a.slice();
res.push.apply(res, b);
res.sort();
return res.filter(function (item, idx, arr) { return idx === 0 || arr[idx - 1] !== item; });
};
Txn.prototype.mergeContext = function (src) {
var _a;
if (src == null) {
return;
}
Expand All @@ -191,13 +200,11 @@ var Txn = (function () {
else if (this.ctx.start_ts !== src.start_ts) {
throw new Error("StartTs mismatch");
}
if (src.keys != null) {
if (this.ctx.keys == null) {
this.ctx.keys = src.keys;
}
else {
(_a = this.ctx.keys).push.apply(_a, src.keys);
}
if (src.keys !== null) {
this.ctx.keys = this.mergeArrays(this.ctx.keys, src.keys);
}
if (src.preds !== null) {
this.ctx.preds = this.mergeArrays(this.ctx.preds, src.preds);
}
};
return Txn;
Expand Down
6 changes: 3 additions & 3 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface Request {
vars?: {
[k: string]: string;
} | null;
startTs?: number | null;
startTs?: number;
}
export interface Response {
data: {};
Expand All @@ -22,7 +22,7 @@ export interface Mutation {
deleteJson?: object | null;
setNquads?: string | null;
deleteNquads?: string | null;
startTs?: number | null;
startTs?: number;
commitNow?: boolean | null;
}
export interface Assigned {
Expand All @@ -40,9 +40,9 @@ export interface Extensions {
}
export interface TxnContext {
start_ts: number;
commit_ts?: number | null;
aborted?: boolean | null;
keys?: string[] | null;
preds?: string[] | null;
}
export interface Latency {
parsing_ns?: number | null;
Expand Down
8 changes: 4 additions & 4 deletions src/clientStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class DgraphClientStub {
headers["X-Dgraph-Vars"] = JSON.stringify(req.vars);
}

return this.callAPI(`query${req.startTs == null ? "" : `/${req.startTs}`}`, {
return this.callAPI(`query${req.startTs === 0 ? "" : `/${req.startTs}`}`, {
method: "POST",
body: req.query,
headers,
Expand All @@ -73,10 +73,10 @@ export class DgraphClientStub {
body = JSON.stringify(obj);
} else if (mu.setNquads != null || mu.deleteNquads != null) {
body = `{
${mu.setNquads == null ? "" : `set {
${mu.setNquads === undefined ? "" : `set {
${mu.setNquads}
}`}
${mu.deleteNquads == null ? "" : `delete {
${mu.deleteNquads === undefined ? "" : `delete {
${mu.deleteNquads}
}`}
}`;
Expand All @@ -92,7 +92,7 @@ export class DgraphClientStub {
headers["X-Dgraph-CommitNow"] = "true";
}

return this.callAPI(`mutate${mu.startTs == null ? "" : `/${mu.startTs}`}`, {
return this.callAPI(`mutate${mu.startTs === 0 ? "" : `/${mu.startTs}`}`, {
method: "POST",
body,
headers,
Expand Down
26 changes: 19 additions & 7 deletions src/txn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export class Txn {

constructor(dc: DgraphClient) {
this.dc = dc;
this.ctx = { start_ts: 0 };
this.ctx = {
start_ts: 0,
keys: [],
preds: [],
};
}

/**
Expand Down Expand Up @@ -184,6 +188,15 @@ export class Txn {
await c.abort(this.ctx);
}

private mergeArrays(a: string[], b: string[]) {
const res = a.slice();
res.push(...b);
res.sort();
// Filter unique in a sorted array.
return res.filter(
(item: string, idx: number, arr: string[]) => idx === 0 || arr[idx - 1] !== item);
}

private mergeContext(src?: TxnContext | null): void {
if (src == null) {
// This condition will be true only if the server doesn't return a txn context after a query or mutation.
Expand All @@ -197,12 +210,11 @@ export class Txn {
throw new Error("StartTs mismatch");
}

if (src.keys != null) {
if (this.ctx.keys == null) {
this.ctx.keys = src.keys;
} else {
this.ctx.keys.push(...src.keys);
}
if (src.keys !== null) {
this.ctx.keys = this.mergeArrays(this.ctx.keys, src.keys);
}
if (src.preds !== null) {
this.ctx.preds = this.mergeArrays(this.ctx.preds, src.preds);
}
}
}
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface Payload {
export interface Request {
query: string;
vars?: { [k: string]: string } | null;
startTs?: number | null;
startTs?: number;
}

export interface Response {
Expand All @@ -24,7 +24,7 @@ export interface Mutation {
deleteJson?: object | null;
setNquads?: string | null;
deleteNquads?: string | null;
startTs?: number | null;
startTs?: number;
commitNow?: boolean | null;
}

Expand All @@ -44,9 +44,9 @@ export interface Extensions {

export interface TxnContext {
start_ts: number;
commit_ts?: number | null;
aborted?: boolean | null;
keys?: string[] | null;
preds?: string[] | null;
}

export interface Latency {
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/acctUpsert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function tryUpsert(account: Account): Promise<void> {
}
}

let startStatus = 0; // set at the start of doUpserts
let startTime = 0; // set at the start of doUpserts
let lastStatus = 0;
let cancelled = false; // cancelled due to timeout

Expand All @@ -76,9 +76,9 @@ let retryCount = 0;

function conditionalLog(): void {
const now = new Date().getTime();
if (now - lastStatus > 1000 && !cancelled) {
if (now - lastStatus > 4000 && !cancelled) {
// tslint:disable-next-line no-console
console.log(`Success: ${successCount}, Retries: ${retryCount}, Total Time: ${now - startStatus} ms`);
console.log(`Success: ${successCount}, Retries: ${retryCount}, Total Time: ${now - startTime} ms`);
lastStatus = now;
}
}
Expand Down Expand Up @@ -111,7 +111,7 @@ async function doUpserts(): Promise<void> {
}
}

startStatus = new Date().getTime();
startTime = new Date().getTime();
const id = setTimeout(
() => {
cancelled = true;
Expand Down
18 changes: 13 additions & 5 deletions tests/integration/bank.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,23 @@ async function createAccounts(): Promise<void> {
});
}

let startStatus = 0; // set before Promise.all
let startTime = 0; // set before Promise.all
let lastStatus = new Date().getTime();
let cancelled = false;
let finished = false;

let runs = 0;
let aborts = 0;

function conditionalLog(): void {
const now = new Date().getTime();
if (now - lastStatus > 4000 && !cancelled) {
// tslint:disable-next-line no-console
console.log(`Runs: ${runs}, Aborts: ${aborts}, Total Time: ${new Date().getTime() - startTime} ms`);
lastStatus = now;
}
}

async function runTotal(): Promise<void> {
const res = await client.newTxn().query(`{
var(func: uid(${uids.join(",")})) {
Expand All @@ -55,9 +65,7 @@ async function runTotal(): Promise<void> {
}`);
// tslint:disable-next-line no-unsafe-any
expect((<{ total: { bal: number }[] }>res.data).total[0].bal).toBe(uids.length * initialBalance);

// tslint:disable-next-line no-console
console.log(`Runs: ${runs}, Aborts: ${aborts}, Total Time: ${new Date().getTime() - startStatus} ms`);
conditionalLog();
}

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

startStatus = new Date().getTime();
startTime = new Date().getTime();
const id = setTimeout(
() => {
cancelled = true;
Expand Down

0 comments on commit b14381c

Please sign in to comment.