Skip to content

Commit

Permalink
fixed some client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
icehaunter committed Dec 9, 2024
1 parent a200ecb commit c531cfc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
8 changes: 6 additions & 2 deletions packages/elixir-client/lib/electric/client/offset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,18 @@ defmodule Electric.Client.Offset do
else
with [tx_offset_str, op_offset_str] <- :binary.split(str, "_"),
{tx_offset, ""} <- Integer.parse(tx_offset_str),
{op_offset, ""} <- Integer.parse(op_offset_str) do
{op_offset, ""} <- parse_int_or_inf(op_offset_str) do
{:ok, %__MODULE__{tx: tx_offset, op: op_offset}}
else
_ -> {:error, "has invalid format"}
end
end
end

defp parse_int_or_inf("inf"), do: {:infinity, ""}
defp parse_int_or_inf(int), do: Integer.parse(int)


@doc """
Create a new #{__MODULE__} struct from the given LSN and operation
offsets.
Expand Down Expand Up @@ -115,7 +119,7 @@ defmodule Electric.Client.Offset do
end

def to_string(%__MODULE__{tx: tx, op: op}) do
"#{Integer.to_string(tx)}_#{Integer.to_string(op)}"
"#{Integer.to_string(tx)}_#{if op == :infinity, do: "inf", else: Integer.to_string(op)}"
end

@spec to_tuple(t()) :: {tx_offset(), op_offset()}
Expand Down
4 changes: 2 additions & 2 deletions packages/elixir-client/test/electric/client/mock_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule Electric.Client.MockTest do
Client.Mock.response(client,
status: 200,
schema: %{id: %{type: "int8"}},
last_offset: Offset.new(0, 0),
last_offset: Offset.new(0, 1),
shape_handle: "my-shape",
body: [
Client.Mock.change(value: %{id: "4444"}),
Expand All @@ -60,7 +60,7 @@ defmodule Electric.Client.MockTest do
%ChangeMessage{value: %{"id" => 2222}},
%ChangeMessage{value: %{"id" => 3333}},
%ChangeMessage{value: %{"id" => 4444}},
up_to_date0()
up_to_date()
] = events
end
end
4 changes: 2 additions & 2 deletions packages/elixir-client/test/support/client_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Support.ClientHelpers do

defmacro offset(tx, op), do: quote(do: %Offset{tx: unquote(tx), op: unquote(op)})

defmacro offset0, do: quote(do: offset(0, 0))
defmacro offset0, do: quote(do: offset(0, :infinity))

defmacro up_to_date() do
quote(do: %ControlMessage{control: :up_to_date, offset: %Offset{tx: _, op: _}})
Expand All @@ -19,5 +19,5 @@ defmodule Support.ClientHelpers do
)
end

defmacro up_to_date0(), do: quote(do: up_to_date(0, 0))
defmacro up_to_date0(), do: quote(do: up_to_date(0, :infinity))
end
18 changes: 12 additions & 6 deletions packages/react-hooks/test/react-hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, expect, inject, it as bareIt } from 'vitest'
import { setTimeout as sleep } from 'node:timers/promises'
import { testWithIssuesTable as it } from './support/test-context'
import { useShape, sortedOptionsHash, UseShapeResult } from '../src/react-hooks'
import { Shape, Message } from '@electric-sql/client'
import { Shape, ShapeStream } from '@electric-sql/client'

const BASE_URL = inject(`baseUrl`)

Expand Down Expand Up @@ -352,13 +352,19 @@ describe(`useShape`, () => {

// Add another row to shape
const [newId] = await insertIssues({ title: `other row` })

Check failure on line 354 in packages/react-hooks/test/react-hooks.test.tsx

View workflow job for this annotation

GitHub Actions / Check packages/react-hooks

'newId' is assigned a value but never used. Allowed unused vars must match /^_/u

const parallelWaiterStream = new ShapeStream({
url: `${BASE_URL}/v1/shape`,
params: {
table: issuesTableUrl,
},
signal: aborter.signal,
subscribe: true,
})

// And wait until it's definitely seen
await waitFor(async () => {
const res = await fetch(
`${BASE_URL}/v1/shape?table=${issuesTableUrl}&offset=-1`
)
const body = (await res.json()) as Message[]
expect(body).toMatchObject([{}, { value: { id: newId } }])
return parallelWaiterStream.isUpToDate || (await sleep(50))
})

await sleep(50)
Expand Down
32 changes: 15 additions & 17 deletions packages/typescript-client/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,8 @@ describe(`HTTP Sync`, () => {
)

await vi.waitFor(async () => {
const res = await fetch(
`${BASE_URL}/v1/shape?table=${tableUrl}&offset=-1`
)
const body = (await res.json()) as Message[]
expect(body.length).greaterThan(1)
await sleep(100)
return client.isUpToDate
})
const updatedData = await client.rows

Expand Down Expand Up @@ -532,11 +529,8 @@ describe(`HTTP Sync`, () => {

// And wait until it's definitely seen
await vi.waitFor(async () => {
const res = await fetch(
`${BASE_URL}/v1/shape?table=${issuesTableUrl}&offset=-1`
)
const body = (await res.json()) as Message[]
expect(body).toHaveLength(12)
await sleep(50)
return issueStream.isUpToDate
})

let catchupOpsCount = 0
Expand All @@ -563,7 +557,9 @@ describe(`HTTP Sync`, () => {
expect(catchupOpsCount).toBe(9)
})

it(`should return correct caching headers`, async ({
// This test doesn't work anymore because server sends initial snapshot as a complete
// stable chunk, so e-tag is the same between requests.
it.skip(`should return correct caching headers`, async ({
issuesTableUrl,
insertIssues,
}) => {
Expand Down Expand Up @@ -603,7 +599,12 @@ describe(`HTTP Sync`, () => {
expect(etagHeader).not.toEqual(etag2Header)
})

it(`should revalidate etags`, async ({ issuesTableUrl, insertIssues }) => {
// This test doesn't work anymore because server sends initial snapshot as a complete
// stable chunk, so e-tag is the same between requests.
it.skip(`should revalidate etags`, async ({
issuesTableUrl,
insertIssues,
}) => {
// Start the shape
await fetch(`${BASE_URL}/v1/shape?table=${issuesTableUrl}&offset=-1`, {})
// Fill it up in separate transactions
Expand Down Expand Up @@ -794,11 +795,8 @@ describe(`HTTP Sync`, () => {

// And wait until it's definitely seen
await vi.waitFor(async () => {
const res = await fetch(
`${BASE_URL}/v1/shape?table=${issuesTableUrl}&offset=-1`
)
const body = (await res.json()) as Message[]
expect(body.length).greaterThan(2)
await sleep(50)
return issueStream.isUpToDate
})

const responseSizes: number[] = []
Expand Down

0 comments on commit c531cfc

Please sign in to comment.