-
Notifications
You must be signed in to change notification settings - Fork 1
/
watchService.test.ts
166 lines (142 loc) · 4.67 KB
/
watchService.test.ts
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
import { constants as http2constants } from 'http2';
import { exec } from 'child_process';
import { HttpClient } from '../../lib/internal/httpClient';
import {
ChangeTypes,
ContentService,
QueryTypes,
RepositoryService,
WatchResult,
WatchService,
} from '../../lib';
const { HTTP_STATUS_NOT_MODIFIED } = http2constants;
const client = new HttpClient({
baseURL: 'http://localhost:36462',
});
const contentService = new ContentService(client);
const repositoryService = new RepositoryService(client);
const sut = new WatchService(client, contentService);
describe('WatchService', () => {
async function sleep(milliseconds: number) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
it('watchFileInner does not return `Not Modified(304)` response', async () => {
const project = 'project1';
const repo = 'repo1';
const path = '/test1.json';
const entry = await contentService.getFile({
project,
repo: 'repo1',
query: {
path,
type: QueryTypes.Identity,
},
});
const revision = entry.revision ?? -1;
try {
const timeoutSeconds = 3;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
await sut.watchFileInner(
project,
repo,
path,
revision,
timeoutSeconds,
);
// If no error occurs, let this test fail
expect(true).toBe(false);
} catch (e) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const statusCode = e.statusCode;
if (statusCode) {
expect(statusCode).toBe(HTTP_STATUS_NOT_MODIFIED);
} else {
// If another error occurs, let this test fail
expect(true).toBe(false);
}
}
}, 10_000);
it('watchFile', async () => {
const project = 'project2';
const repo = 'repo2';
const filePath = '/test8.json';
const emitter = await sut.watchFile({
project,
repo,
filePath,
});
let count = 0;
emitter.on('data', (data: WatchResult) => {
count++;
console.log(`data=${JSON.stringify(data)}`);
expect(data.entry.path).toBe(filePath);
});
emitter.on('error', (e) => {
console.log(`error=${JSON.stringify(e)}`);
throw e;
});
setTimeout(() => {
// The target updates the json three times
exec('make update-test-data-for-watchFile', (e) => {
if (e) {
// fail
expect(true).toBe(false);
}
});
}, 1_000);
await sleep(15_000);
expect(count).toBe(4); // initial entry + updated three times = 4 times
}, 30_000);
it('watchRepo', async () => {
const project = 'project5';
const repoName = 'repo1';
const pathPattern = '/**';
const repos = await repositoryService.list(project);
expect(repos.length).toBe(3);
const repo = repos.filter((repo) => repo.name === repoName);
const lastKnownRevision = repo[0].headRevision ?? 1;
const emitter = await sut.watchRepo({
project,
repo: repoName,
pathPattern,
lastKnownRevision,
});
let count = 0;
emitter.on('data', (data: WatchResult) => {
count++;
console.log(`data=${JSON.stringify(data)}`);
// expect(data.entry.path).toBe(filePath);
});
emitter.on('error', (e) => {
console.log(`error=${JSON.stringify(e)}`);
throw e;
});
setTimeout(() => {
// The target updates the json three times
exec('make update-test-data-for-watchRepo', (e) => {
if (e) {
// fail
expect(true).toBe(false);
}
});
}, 1_000);
await sleep(20_000);
expect(count).toBe(4); // initial entry + updated three times = 4 times
await contentService.push({
project,
repo: repoName,
baseRevision: 'HEAD',
commitMessage: {
summary: 'Remove the test file',
},
changes: [
{
path: '/a.json',
type: ChangeTypes.Remove,
},
],
});
}, 30_000);
});