-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.test.ts
214 lines (178 loc) · 6.62 KB
/
mod.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
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
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { withSafeAtomics } from "./mod.ts";
Deno.test("withSafeAtomics monkey patches Deno.Kv", async () => {
// Arrange
const mockKey = ["test", "abc"];
const mockValue = "123";
// Act
const kv = withSafeAtomics(await Deno.openKv());
await kv.set(mockKey, mockValue);
const { value } = await kv.get(mockKey);
await kv.close();
// Assert
assertEquals(value, mockValue);
});
Deno.test("setSafeAtomic update the values", async () => {
// Arrange
const mockKey = ["test", "abc"];
const mockValue = "123";
const updateValue = (_value: unknown): unknown => {
return mockValue;
};
// Act
const kv = withSafeAtomics(await Deno.openKv());
const result = await kv.setSafeAtomic(mockKey, updateValue);
const { value: newValue } = await kv.get(mockKey);
await kv.close();
// Assert
assertEquals(newValue, mockValue);
assertEquals(result, { ok: true, error: null, value: newValue });
});
Deno.test("setSafeAtomicMany updates multiple values", async () => {
// Arrange
const mockKey1 = ["test", "key 1"];
const mockKey2 = ["test", "key 2"];
const mockUpdatedValue1 = "new value 1";
const mockUpdatedValue2 = "new value 2";
const mockUpdatedValues = [mockUpdatedValue1, mockUpdatedValue2];
const keys = [mockKey1, mockKey2];
const updateValues = () => mockUpdatedValues;
const retryCount = 3;
// Act
const kv = withSafeAtomics(await Deno.openKv());
const result = await kv.setSafeAtomicMany(keys, updateValues, retryCount);
const { value: newValue1 } = await kv.get(mockKey1);
const { value: newValue2 } = await kv.get(mockKey2);
await kv.close();
// Assert
assertEquals(newValue1, mockUpdatedValue1);
assertEquals(newValue2, mockUpdatedValue2);
assertEquals(result, { ok: true, error: null, values: [newValue1, newValue2] });
});
Deno.test(
"setSafeAtomic does not update the value if abort() is called",
async () => {
// Arrange
const mockKey = ["test", "abc"];
const mockValue = "123";
const mockUpdatedValue = "new value";
const abortReason = "Testing";
const updateValue = (_value: unknown, abort: (reason?: string) => void): unknown => {
abort(abortReason);
return mockUpdatedValue;
};
// Act
const kv = withSafeAtomics(await Deno.openKv());
await kv.set(mockKey, mockValue);
const result = await kv.setSafeAtomic(mockKey, updateValue);
const { value: newValue } = await kv.get(mockKey);
await kv.close();
// Assert
assertEquals(newValue, mockValue);
assertEquals(result, { ok: false, error: abortReason, value: mockValue });
},
);
Deno.test(
"setSafeAtomicMany does not update any values if abort() is called",
async () => {
// Arrange
const mockKey1 = ["test", "key 1"];
const mockKey2 = ["test", "key 2"];
const mockValue1 = "value 1";
const mockValue2 = "value 2";
const mockUpdatedValue1 = "new value 1";
const mockUpdatedValue2 = "new value 2";
const mockUpdatedValues = [mockUpdatedValue1, mockUpdatedValue2];
const abortReason = "Testing";
const keys = [mockKey1, mockKey2];
const updateValues = (
_values: unknown[],
abort: (reason?: string) => void,
): unknown[] | void => {
abort(abortReason);
return mockUpdatedValues;
};
const retryCount = 3;
// Set initial values
const kv = withSafeAtomics(await Deno.openKv());
await kv.set(mockKey1, mockValue1);
await kv.set(mockKey2, mockValue2);
// Act
const result = await kv.setSafeAtomicMany(keys, updateValues, retryCount);
const { value: newValue1 } = await kv.get(mockKey1);
const { value: newValue2 } = await kv.get(mockKey2);
await kv.close();
// Assert
assertEquals(newValue1, mockValue1);
assertEquals(newValue2, mockValue2);
assertEquals(result, { ok: false, error: abortReason, values: [mockValue1, mockValue2] });
},
);
// TODO: test the retries more thoroughly
// Deno.test(
// "setSafeAtomicMany retries if values change in the mean time, and updates with latest values",
// async () => {
// /// Arrange
// const mockBalanceKey = ["test", "balance"];
// // We'll start off with an initial balance of 100
// const mockInitialBalance = 100;
// // And try to subtract 50 from it
// const mockSubtractBalance = 50;
// // But in the mean time, another process will set it to 25
// const mockAlteredBalance = 250;
// const originalDenoKv = await Deno.openKv();
// let enableCommitMock = true;
// const denoKvProxy = new Proxy(originalDenoKv, {
// get: (target, prop, receiver) => {
// // Overwrite .atomic to return a new proxy
// // @ts-ignore
// if (prop !== "atomic") return target[prop];
// const originalAtomic = target[prop].bind(target);
// return new Proxy(originalAtomic, {
// get: (target, prop, receiver) => {
// // If {enableCommitMock} is true, make .commit return a function
// // that sets the balance to 25 and then runs the real .commit
// if (prop !== "commit" || !enableCommitMock) {
// // @ts-ignore
// return target[prop];
// }
// return async function mockCommit(this: Deno.AtomicOperation) {
// // Alter the balance before the Deno KV transaction can commit
// const kv2 = await Deno.openKv();
// await kv2.set(
// mockBalanceKey,
// ((await kv.get(mockBalanceKey)).value as number) + 1
// );
// await kv2.close();
// enableCommitMock = false;
// return this.commit();
// };
// },
// });
// },
// });
// // Mock {kv.atomic#commit} to set the balance to 25 and then unmock itself
// const kv = withSafeAtomics(denoKvProxy);
// // Set initial value
// await kv.set(mockBalanceKey, mockInitialBalance);
// const keys = [mockBalanceKey];
// const updateValues = (
// entries: DenoKvEntry[],
// abort: () => void
// ): unknown[] | void => {
// const balance = entries[0][1] as number;
// console.log(entries, balance, mockSubtractBalance);
// if (balance < mockSubtractBalance) {
// console.log("aborting");
// abort();
// }
// return [balance - mockSubtractBalance];
// };
// // Act
// await kv.setSafeAtomicMany(keys, updateValues);
// const { value: newBalance } = await kv.get(mockBalanceKey);
// await kv.close();
// // Assert
// assertEquals(newBalance, mockAlteredBalance - mockSubtractBalance);
// }
// );