Skip to content

Commit 055f046

Browse files
committed
add query timestamp tests
1 parent 086d004 commit 055f046

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
18+
const assert = require('assert');
19+
const requests = require('../../lib/requests');
20+
const Encoder = require('../../lib/encoder');
21+
const types = require('../../lib/types');
22+
const utils = require('../../lib/utils');
23+
const DefaultExecutionOptions = require('../../lib/execution-options').DefaultExecutionOptions;
24+
const ExecutionProfile = require('../../lib/execution-profile').ExecutionProfile;
25+
const QueryRequest = requests.QueryRequest;
26+
const ExecuteRequest = requests.ExecuteRequest;
27+
const BatchRequest = requests.BatchRequest;
28+
const defaultOptions = require('../../lib/client-options').defaultOptions;
29+
30+
const encoder = new Encoder(types.protocolVersion.maxSupported, {});
31+
32+
describe('ExecuteRequest', function () {
33+
describe('#write()', function() {
34+
const queryOptions = { fetchSize: 0 };
35+
testGenerateOnce(queryOptions, getExecuteRequest, getExecuteRequestExpectedBuffer);
36+
testGenerate(queryOptions, getExecuteRequest, getExecuteRequestExpectedBuffer);
37+
});
38+
});
39+
40+
describe('QueryRequest', function () {
41+
describe('#write()', function () {
42+
const queryOptions = { fetchSize: 0 };
43+
testGenerateOnce(queryOptions, getQueryRequest, getQueryRequestExpectedBuffer);
44+
testGenerate(queryOptions, getQueryRequest, getQueryRequestExpectedBuffer);
45+
});
46+
});
47+
48+
describe('BatchRequest', function () {
49+
describe('#write()', function () {
50+
const queryOptions = { logged: false, consistency: 1 };
51+
testGenerateOnce(queryOptions, getBatchRequest, getBatchRequestExpectedBuffer);
52+
testGenerate(queryOptions, getBatchRequest, getBatchRequestExpectedBuffer);
53+
});
54+
});
55+
56+
function testGenerateOnce(queryOptions, requestGetter, bufferGetter) {
57+
it('should generate the timestamp once', function () {
58+
assert.strictEqual(queryOptions.timestamp, undefined);
59+
const client = getClientFake();
60+
const request = requestGetter(client, queryOptions);
61+
62+
let called = 0;
63+
const write = () => {
64+
called++;
65+
return request.write(encoder, 0);
66+
};
67+
68+
const nbCalls = 4;
69+
for (let i = 0; i < nbCalls; i++) {
70+
assert.deepEqual(write(), bufferGetter(0));
71+
}
72+
assert.strictEqual(called, nbCalls);
73+
});
74+
}
75+
76+
function testGenerate(queryOptions, requestGetter, bufferGetter) {
77+
it('should generate the timestamp', function () {
78+
assert.strictEqual(queryOptions.timestamp, undefined);
79+
const client = getClientFake();
80+
81+
let called = 0;
82+
const write = (request) => {
83+
called++;
84+
return request.write(encoder, 0);
85+
};
86+
87+
const nbCalls = 4;
88+
for (let i = 0; i < nbCalls; i++) {
89+
const request = requestGetter(client, queryOptions);
90+
assert.deepEqual(write(request), bufferGetter(i));
91+
}
92+
assert.strictEqual(called, nbCalls);
93+
});
94+
}
95+
96+
function getExecuteRequestExpectedBuffer(timestamp) {
97+
return Buffer.concat([
98+
utils.allocBufferFromArray([
99+
types.protocolVersion.maxSupported,
100+
0, 0, 0, 0xA, // flags + stream id + opcode (0xA = execute)
101+
0, 0, 0, 0x16, // length
102+
0, 2, 0x51, 0x31, // id length = 2 + id (Q1)
103+
0, 2, 0x52, 0x31, // result id length = 2 + id (Q1)
104+
0, 1, 0, 0, 0, 0x20, // consistency level + flags (0x20 = timestamp)
105+
]),
106+
longBuffer(timestamp)
107+
]);
108+
}
109+
110+
function getQueryRequestExpectedBuffer(timestamp) {
111+
return Buffer.concat([
112+
utils.allocBufferFromArray([
113+
types.protocolVersion.maxSupported,
114+
0, 0, 0, 0x7, // flags + stream id + opcode (0x7 = query)
115+
0, 0, 0, 0x14, // length
116+
0, 0, 0, 2, 0x51, 0x31, // query, length = 2, 'Q1'
117+
0, 1, 0, 0, 0, 0x20, // consistency level + flags (0x20 = timestamp)
118+
]),
119+
longBuffer(timestamp)
120+
]);
121+
}
122+
123+
function getBatchRequestExpectedBuffer(timestamp) {
124+
return Buffer.concat([
125+
utils.allocBufferFromArray([
126+
types.protocolVersion.maxSupported,
127+
0, 0, 0, 0xD, // flags + stream id + opcode (0xD = batch)
128+
0, 0, 0, 0x23, // length
129+
1, 0, 2, // 1 = unlogged, 2 queries
130+
0, 0, 0, 0, 2, 0x51, 0x31, 0, 0, // simple query, length = 2, 'Q1', 0 values
131+
0, 0, 0, 0, 2, 0x51, 0x32, 0, 0, // simple query, length = 2, 'Q2', 0 values
132+
0, 1, 0, 0, 0, 0x20, // consistency level + flags (0x20 = timestamp)
133+
]),
134+
longBuffer(timestamp)
135+
]);
136+
}
137+
138+
function longBuffer(value) {
139+
value = types.Long.fromNumber(value);
140+
return types.Long.toBuffer(value);
141+
}
142+
143+
function getExecuteRequest(client, options) {
144+
const execOptions = DefaultExecutionOptions.create(options, client);
145+
const meta = { resultId: utils.allocBufferFromString('R1'), columns: [ { } ] };
146+
return new ExecuteRequest('Q1', utils.allocBufferFromString('Q1'), [], execOptions, meta);
147+
}
148+
149+
function getQueryRequest(client, options) {
150+
const execOptions = DefaultExecutionOptions.create(options, client);
151+
return new QueryRequest('Q1', [], execOptions);
152+
}
153+
154+
function getBatchRequest(client, options) {
155+
const execOptions = DefaultExecutionOptions.create(options, client);
156+
return new BatchRequest(
157+
[
158+
{ query: 'Q1', params: [] },
159+
{ query: 'Q2', params: [] }
160+
], execOptions);
161+
}
162+
163+
function getClientFake() {
164+
const clientOptions = defaultOptions();
165+
let timestamp = 0;
166+
clientOptions.policies.timestampGeneration.next = () => timestamp++;
167+
return {
168+
profileManager: { getProfile: x => new ExecutionProfile(x || 'default') },
169+
options: clientOptions,
170+
controlConnection: { protocolVersion: types.protocolVersion.maxSupported }
171+
};
172+
}

0 commit comments

Comments
 (0)