-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DX-1019: Read Your Writes Support (#1175)
* add: readYourWrites option interface * send local sync token on requests * fmt * add promise.all tests * add: lua script test * format tests * fmt * change upstashSyncToken convention * add public redis client test * add: fastly and cloudflare clients ryw support * fmt * add default test * add: comments * add: http comment * sync token docs * remove comment * fix readYourWrites arg comment * add: ryw operation comments * revert requester * revert requester interface
- Loading branch information
1 parent
26a3a66
commit 90ae6b1
Showing
9 changed files
with
192 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { keygen, newHttpClient } from "./test-utils"; | ||
|
||
import { afterAll, describe, expect, test } from "bun:test"; | ||
|
||
import { Redis as PublicRedis } from "../platforms/nodejs"; | ||
import { SetCommand } from "./commands/set"; | ||
import { Redis } from "./redis"; | ||
|
||
const client = newHttpClient(); | ||
const { cleanup } = keygen(); | ||
afterAll(cleanup); | ||
describe("Read Your Writes Feature", () => { | ||
test("successfully retrieves Upstash-Sync-Token in the response header and updates local state", async () => { | ||
const initialSync = client.upstashSyncToken; | ||
await new SetCommand(["key", "value"]).exec(client); | ||
const updatedSync = client.upstashSyncToken; | ||
await new SetCommand(["key", "value"]).exec(client); | ||
|
||
expect(updatedSync).not.toEqual(initialSync); | ||
}); | ||
|
||
test("succesfully updates sync state with pipeline", async () => { | ||
const initialSync = client.upstashSyncToken; | ||
|
||
const { pipeline } = new Redis(client); | ||
const p = pipeline(); | ||
|
||
p.set("key1", "value1"); | ||
p.set("key2", "value2"); | ||
p.set("key3", "value3"); | ||
|
||
await p.exec(); | ||
|
||
const updatedSync = client.upstashSyncToken; | ||
|
||
expect(initialSync).not.toEqual(updatedSync); | ||
}); | ||
|
||
test("updates after each element of promise.all", async () => { | ||
let currentSync = client.upstashSyncToken; | ||
|
||
const promises = Array.from({ length: 3 }, (_, i) => | ||
new SetCommand([`key${i}`, `value${i}`]).exec(client).then(() => { | ||
expect(client.upstashSyncToken).not.toEqual(currentSync); | ||
currentSync = client.upstashSyncToken; | ||
}), | ||
); | ||
|
||
await Promise.all(promises); | ||
}); | ||
|
||
test("updates after successful lua script call", async () => { | ||
const s = `redis.call('SET', 'mykey', 'myvalue') | ||
return 1 | ||
`; | ||
|
||
const initialSync = client.upstashSyncToken; | ||
|
||
const redis = new Redis(client); | ||
const script = redis.createScript(s); | ||
|
||
await script.exec([], []); | ||
|
||
const updatedSync = client.upstashSyncToken; | ||
|
||
expect(updatedSync).not.toEqual(initialSync); | ||
}); | ||
|
||
test("should not update the sync state in case of Redis client with manuel HTTP client and opt-out ryw", async () => { | ||
const optOutClient = newHttpClient(); | ||
const redis = new Redis(optOutClient, { readYourWrites: false }); | ||
|
||
const initialSync = optOutClient.upstashSyncToken; | ||
|
||
await redis.set("key", "value"); | ||
|
||
const updatedSync = optOutClient.upstashSyncToken; | ||
|
||
expect(updatedSync).toEqual(initialSync); | ||
}); | ||
|
||
test("should not update the sync state when public Redis interface is provided with opt-out", async () => { | ||
const redis = new PublicRedis({ | ||
url: process.env.UPSTASH_REDIS_REST_URL, | ||
token: process.env.UPSTASH_REDIS_REST_TOKEN, | ||
readYourWrites: false, | ||
}); | ||
|
||
// @ts-expect-error - We need the sync token for this test, which resides on the client | ||
const initialSync = redis.client.upstashSyncToken; | ||
|
||
await redis.set("key", "value"); | ||
|
||
// @ts-expect-error - We need the sync token for this test, which resides on the client | ||
const updatedSync = redis.client.upstashSyncToken; | ||
|
||
expect(updatedSync).toEqual(initialSync); | ||
}); | ||
|
||
test("should update the sync state when public Redis interface is provided with default behaviour", async () => { | ||
const redis = new PublicRedis({ | ||
url: process.env.UPSTASH_REDIS_REST_URL, | ||
token: process.env.UPSTASH_REDIS_REST_TOKEN, | ||
}); | ||
|
||
// @ts-expect-error - We need the sync token for this test, which resides on the client | ||
const initialSync = redis.client.upstashSyncToken; | ||
|
||
await redis.set("key", "value"); | ||
|
||
// @ts-expect-error - We need the sync token for this test, which resides on the client | ||
const updatedSync = redis.client.upstashSyncToken; | ||
expect(updatedSync).not.toEqual(initialSync); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters