FakeRealtime

The server's row changes a test reports to an in-memory client, in place of a Realtime channel:

var tasks = JsonArray(emptyList()) // the server's rows: the engine answers every read with them
val fakeRealtime = FakeRealtime()
val supabase = createSupabaseClient(url, key) {
httpEngine = MockEngine { respond(tasks.toString(), headers = jsonHeaders) }
install(Postgrest)
install(Offline) {
inMemory(realtime = fakeRealtime) // no install(Realtime) needed
realtime { table("tasks") }
}
}
fakeRealtime.joinCount.first { it > 0 }
val milk = buildJsonObject { put("id", "t1"); put("title", "Milk") }
tasks = JsonArray(tasks + milk) // the row is on the server, then its change is sent
fakeRealtime.reportInsert("tasks", milk)

The client joins a channel for each named table as it would with Realtime: while a user is signed in (or Auth is not installed), the network is not known down and Offline.disableRealtime has not switched it off. The fake answers each join at once. subscribedTables says which tables the client follows as soon as it opens their channels, and joinCount moves once the client has handled a join: it has reported RealtimeStatus.Subscribed and asked for the joined tables' cached queries to be read again, since nothing is replayed. Wait for joinCount before reporting a change, or the join may be handled after it. failTable refuses one table's channel, as a server refuses a binding, and leaves the others joined.

The test's MockEngine plays the server, and the fake only the channel: the client reads the named tables from the engine on every join, and reads a query again after a change that met a read in flight or that it cannot apply to the query exactly. So the engine must answer with the rows as they are after each change the test reports, as a server would; one that still answers with the rows from before has those reads take the change back out of the cache.

A report reaches the client as Realtime would send it. A report to a table no joined channel follows is dropped, as a server drops a change for a channel nobody joined, and so is an insert or an update whose record does not meet the table's RealtimeFilter, which the server evaluates before it sends; both are counted in droppedReportCount. A delete reaches the client whatever the filter, as Realtime sends a delete to every subscriber of the table. Nothing is replayed: a change reported while the client was not joined never reaches it.

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard

How many reports were dropped: those that found no joined channel following their table, and the inserts and updates whose record does not meet the table's RealtimeFilter.

Link copied to clipboard
val joinCount: StateFlow<Int>

How many joins the client has handled, every channel counted: it has reported the channel RealtimeStatus.Subscribed and asked for the named tables' cached queries to be read again, though those reads may still be in flight. A test waits for it to move, after the client opens and after each failChannel, dropChannel, failTable or switch back on, before it reports a change.

Link copied to clipboard
val subscribedTables: StateFlow<Set<String>>

The tables the client follows through a joined channel now, each as realtime { } keys it: its name alone in the client's default schema, schema.table in another. A table is missing while its channel is not joined: before the client joins, while it is paused or switched off, and between a channel that ended and the next join of that table.

Functions

Link copied to clipboard

Ends every joined channel quietly, as a lost socket does: the client reports RealtimeStatus.Stalled, joins each table again after a short wait, and reads it again. Nothing happens while no channel is joined.

Link copied to clipboard

Ends every joined channel with cause, as a server that refuses the joins or closes the channels: the client reports RealtimeStatus.Stalled with cause, joins each table again after a wait of at most a minute (a second the first time), and reads it again. Nothing happens while no channel is joined.

Link copied to clipboard
fun failTable(table: String, cause: Throwable, schema: String? = null)

Ends the channel of table alone with cause, as a server that refuses that table's binding: Offline.realtimeTableStatus reads RealtimeStatus.Stalled with cause for it, the other tables stay joined and keep receiving reports, and the client joins table again after a wait of at most a minute (a second the first time), and reads it again. Nothing happens while table is not joined.

Link copied to clipboard
fun reportDelete(table: String, oldRecord: JsonObject, schema: String? = null)

Reports a row of table deleted on the server; the client removes the row oldRecord keys from every cached query of the table. The table's RealtimeFilter does not apply: Realtime sends a delete to every subscriber of the table.

Link copied to clipboard
fun reportInsert(table: String, record: JsonObject, schema: String? = null)

Reports a row of table inserted on the server; the client applies record to every cached query of the table, as it applies a Realtime record. Dropped when record does not meet the table's RealtimeFilter.

Link copied to clipboard
fun reportUpdate(table: String, record: JsonObject, schema: String? = null)

Reports a row of table updated on the server; the client applies record as it applies a Realtime record. Dropped when record, the row after the update, does not meet the table's RealtimeFilter, as the server sends no change of a row the update moved out of it.