|
| 1 | +import { keygen, newHttpClient } from "./test-utils"; |
| 2 | + |
| 3 | +import { afterAll, describe, expect, test } from "bun:test"; |
| 4 | + |
| 5 | +import { Redis as PublicRedis } from "../platforms/nodejs"; |
| 6 | +import { SetCommand } from "./commands/set"; |
| 7 | +import { Redis } from "./redis"; |
| 8 | + |
| 9 | +const client = newHttpClient(); |
| 10 | +const { cleanup } = keygen(); |
| 11 | +afterAll(cleanup); |
| 12 | +describe("Read Your Writes Feature", () => { |
| 13 | + test("successfully retrieves Upstash-Sync-Token in the response header and updates local state", async () => { |
| 14 | + const initialSync = client.upstashSyncToken; |
| 15 | + await new SetCommand(["key", "value"]).exec(client); |
| 16 | + const updatedSync = client.upstashSyncToken; |
| 17 | + await new SetCommand(["key", "value"]).exec(client); |
| 18 | + |
| 19 | + expect(updatedSync).not.toEqual(initialSync); |
| 20 | + }); |
| 21 | + |
| 22 | + test("succesfully updates sync state with pipeline", async () => { |
| 23 | + const initialSync = client.upstashSyncToken; |
| 24 | + |
| 25 | + const { pipeline } = new Redis(client); |
| 26 | + const p = pipeline(); |
| 27 | + |
| 28 | + p.set("key1", "value1"); |
| 29 | + p.set("key2", "value2"); |
| 30 | + p.set("key3", "value3"); |
| 31 | + |
| 32 | + await p.exec(); |
| 33 | + |
| 34 | + const updatedSync = client.upstashSyncToken; |
| 35 | + |
| 36 | + expect(initialSync).not.toEqual(updatedSync); |
| 37 | + }); |
| 38 | + |
| 39 | + test("updates after each element of promise.all", async () => { |
| 40 | + let currentSync = client.upstashSyncToken; |
| 41 | + |
| 42 | + const promises = Array.from({ length: 3 }, (_, i) => |
| 43 | + new SetCommand([`key${i}`, `value${i}`]).exec(client).then(() => { |
| 44 | + expect(client.upstashSyncToken).not.toEqual(currentSync); |
| 45 | + currentSync = client.upstashSyncToken; |
| 46 | + }), |
| 47 | + ); |
| 48 | + |
| 49 | + await Promise.all(promises); |
| 50 | + }); |
| 51 | + |
| 52 | + test("updates after successful lua script call", async () => { |
| 53 | + const s = `redis.call('SET', 'mykey', 'myvalue') |
| 54 | + return 1 |
| 55 | + `; |
| 56 | + |
| 57 | + const initialSync = client.upstashSyncToken; |
| 58 | + |
| 59 | + const redis = new Redis(client); |
| 60 | + const script = redis.createScript(s); |
| 61 | + |
| 62 | + await script.exec([], []); |
| 63 | + |
| 64 | + const updatedSync = client.upstashSyncToken; |
| 65 | + |
| 66 | + expect(updatedSync).not.toEqual(initialSync); |
| 67 | + }); |
| 68 | + |
| 69 | + test("should not update the sync state in case of Redis client with manuel HTTP client and opt-out ryw", async () => { |
| 70 | + const optOutClient = newHttpClient(); |
| 71 | + const redis = new Redis(optOutClient, { readYourWrites: false }); |
| 72 | + |
| 73 | + const initialSync = optOutClient.upstashSyncToken; |
| 74 | + |
| 75 | + await redis.set("key", "value"); |
| 76 | + |
| 77 | + const updatedSync = optOutClient.upstashSyncToken; |
| 78 | + |
| 79 | + expect(updatedSync).toEqual(initialSync); |
| 80 | + }); |
| 81 | + |
| 82 | + test("should not update the sync state when public Redis interface is provided with opt-out", async () => { |
| 83 | + const redis = new PublicRedis({ |
| 84 | + url: process.env.UPSTASH_REDIS_REST_URL, |
| 85 | + token: process.env.UPSTASH_REDIS_REST_TOKEN, |
| 86 | + readYourWrites: false, |
| 87 | + }); |
| 88 | + |
| 89 | + // @ts-expect-error - We need the sync token for this test, which resides on the client |
| 90 | + const initialSync = redis.client.upstashSyncToken; |
| 91 | + |
| 92 | + await redis.set("key", "value"); |
| 93 | + |
| 94 | + // @ts-expect-error - We need the sync token for this test, which resides on the client |
| 95 | + const updatedSync = redis.client.upstashSyncToken; |
| 96 | + |
| 97 | + expect(updatedSync).toEqual(initialSync); |
| 98 | + }); |
| 99 | + |
| 100 | + test("should update the sync state when public Redis interface is provided with default behaviour", async () => { |
| 101 | + const redis = new PublicRedis({ |
| 102 | + url: process.env.UPSTASH_REDIS_REST_URL, |
| 103 | + token: process.env.UPSTASH_REDIS_REST_TOKEN, |
| 104 | + }); |
| 105 | + |
| 106 | + // @ts-expect-error - We need the sync token for this test, which resides on the client |
| 107 | + const initialSync = redis.client.upstashSyncToken; |
| 108 | + |
| 109 | + await redis.set("key", "value"); |
| 110 | + |
| 111 | + // @ts-expect-error - We need the sync token for this test, which resides on the client |
| 112 | + const updatedSync = redis.client.upstashSyncToken; |
| 113 | + expect(updatedSync).not.toEqual(initialSync); |
| 114 | + }); |
| 115 | +}); |
0 commit comments