-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathspoolchanges.js
More file actions
214 lines (192 loc) · 7.79 KB
/
spoolchanges.js
File metadata and controls
214 lines (192 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright © 2017, 2024 IBM Corp. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/* global afterEach beforeEach describe it */
const assert = require('assert');
const nock = require('nock');
const http = require('node:http');
const { newClient } = require('../includes/request.js');
const spoolchanges = require('../includes/spoolchanges.js');
const { convertError } = require('../includes/error.js');
const host = 'localhost';
// To avoid clashes between multiple runs use a given port (converted to a number) if configured
const port = +process.env.COUCHBACKUP_MOCK_SERVER_PORT || 7777;
const url = `http://${host}:${port}`;
const dbName = 'fakenockdb';
const longTestTimeout = 3000;
const dbClient = newClient(`${url}/${dbName}`, { parallelism: 1 });
const seqSuffix = Buffer.alloc(124, 'abc123').toString('base64');
function changes(bufferSize, tolerance) {
// Make a pipeline of the spool changes source streams
return spoolchanges(dbClient, '/dev/null', () => {}, bufferSize, tolerance)
// Historically spool changes itself could return an error, but
// now it returns a pipeline promise.
// Error conversion takes place in the top level functions
// so to facilitate unit testing we just do the same conversion here.
.catch((e) => { throw convertError(e); });
}
describe('Check spool changes', function() {
describe('#unit error cases', function() {
it('should terminate on request error', async function() {
nock(url)
.post(`/${dbName}/_changes`)
.query(true)
.times(3)
.replyWithError({ code: 'ECONNRESET', message: 'socket hang up' });
// Note this is setting changes follower tolerance to 0
// so that the error is not suppressed beyond 3 configured retries
// in the underlying SDK call, follower will not retry
return changes(500, 0).catch((err) => {
assert.strictEqual(err.name, 'Error');
assert.strictEqual(err.message, `socket hang up: post ${url}/${dbName}/_changes ECONNRESET`);
assert.ok(nock.isDone());
});
}).timeout(longTestTimeout);
it('should terminate on bad HTTP status code response', async function() {
nock(url)
.post(`/${dbName}/_changes`)
.query(true)
.times(3)
.reply(500, function(uri, requestBody) {
this.req.response.statusMessage = 'Internal Server Error';
return { error: 'foo', reason: 'bar' };
});
// Note this is setting changes follower tolerance to 0
// so that the error is not suppressed beyond 3 configured retries
// in the underlying SDK call, follower will not retry
return changes(500, 0).catch((err) => {
assert.strictEqual(err.name, 'HTTPFatalError');
assert.strictEqual(err.message, `500 post ${url}/${dbName}/_changes - Error: foo: bar`);
assert.ok(nock.isDone());
});
}).timeout(longTestTimeout);
});
describe('success cases', function() {
let server;
let batchSize;
let totalChanges;
let fullResponse = false;
let sparseResultsArray;
let remainingMockCalls;
let pending;
beforeEach('Start server', function(done) {
server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(getReply());
});
server.listen(port, host, done);
});
afterEach('Stop server', function(done) {
server.closeAllConnections();
server.close(done);
});
function setupTestSize() {
remainingMockCalls = totalChanges / batchSize + (totalChanges % batchSize > 0 ? 1 : 0);
pending = totalChanges;
sparseResultsArray = (!fullResponse)
? Array(batchSize).fill({
seq: null,
id: 'doc',
changes: [{ rev: '1-abcdef0123456789abcdef0123456789' }]
})
: [];
}
function makeChangeItem(seq, index) {
return {
seq: `${seq + index}-${seqSuffix}`,
id: `doc${seq + index}`,
changes: [{ rev: '1-abcdef0123456789abcdef0123456789' }]
};
}
function getResults(batchSize, seq) {
return Array.from(Array(batchSize).fill(seq), makeChangeItem);
}
function getReply() {
remainingMockCalls--;
pending -= batchSize;
const lastSeq = (totalChanges - pending);
const seq = lastSeq - batchSize;
return JSON.stringify({
results: fullResponse ? getResults(batchSize, seq) : sparseResultsArray,
pending,
last_seq: `${lastSeq}-abc`
});
}
describe('#unit shorter spool changes checks', function() {
it('should keep collecting changes', async function() {
// This test validates that spooling will correctly
// continue across multiple requests
// (4 batches of 10000 to be precise).
// This test might take up to 10 seconds
this.timeout(10 * 1000);
// Use full changes for this test
batchSize = 10000;
totalChanges = 40000;
fullResponse = true;
setupTestSize();
return changes(500).then(() => {
assert.equal(remainingMockCalls, 0, 'There should be the correct number of mock calls.');
});
});
it('should keep collecting sparse changes', async function() {
// This test checks that making thousands of requests doesn't
// make anything bad happen.
// This test might take up to 25 seconds
this.timeout(25 * 1000);
// Use sparse changes for this test and a response batch size of 1
// This means that each mock changes request will return only 1 change.
batchSize = 1;
totalChanges = 2500;
fullResponse = false;
setupTestSize();
// We collect the changes in the standard batches of 500.
return changes(500).then(() => {
assert.equal(remainingMockCalls, 0, 'There should be the correct number of mock calls.');
});
});
});
describe('Longer spool changes checks', function() {
it('#slow should keep collecting changes (25M)', async function() {
// This test might take up to 5 minutes
this.timeout(5 * 60 * 1000);
// Note changes spooling uses a constant batch size of 10k.
// We set the same batch size for generated responses here.
batchSize = 10000;
totalChanges = 25000000;
fullResponse = false;
setupTestSize();
// Use sparse changes for this test and collect in batches
// matching the response size of 10k.
return changes(batchSize).then(() => {
assert.equal(remainingMockCalls, 0, 'There should be the correct number of mock calls.');
});
});
it('#slower should keep collecting changes (500M)', async function() {
// This test might take up to 90 minutes
this.timeout(90 * 60 * 1000);
// Note changes spooling uses a constant batch size of 10k.
// We set a matching batch size here.
batchSize = 10000;
totalChanges = 500000000;
// Use full changes for this test to exercise load
fullResponse = true;
setupTestSize();
// We collect the changes in batches
// matching the response size of 10k.
return changes(batchSize).then(() => {
assert.equal(remainingMockCalls, 0, 'There should be the correct number of mock calls.');
});
});
});
});
});