FakeDatabaseServer class final
A server for tests that sits at the connection seam and answers only what the test tells it to.
Pass it to databaseWithFakeServer. Requests are recorded in requests; listens, writes and gets wait
until the test answers them. Drive time with fakeAsync.
It behaves as the real connection does around it: the first connection is made in a timer after the
database's first operation (or goOnline), not when it is created; requests made while offline are queued and sent when the connection comes up,
after the listens are sent again; a dropped connection is retried after the database's reconnect delay
for as long as acceptConnections is false.
The test must answer writes in the order they left: answering a put, merge or onDisconnect
request while one sent before it on the same connection is unanswered throws a StateError
naming both. A request made while connected leaves at once; after a reconnect the unanswered
puts and merges leave first, then the onDisconnect requests made while offline, as the real
connection replays them. ackNextWrite, failNextWrite and ackNextOnDisconnect answer the
oldest request of their kind, and throw when an older request of the other kind is
unanswered; the *Last* helpers answer the newest of their kind and take outOfOrder: true
for a test that models something a server does not do.
That the server answers writes in send order is inferred from the emulator probe
(rtdb_emulator_probe.md, addendum 2 item 4): a pipelined auth, listen, get, unauth, get
was answered in order; no write was in that burst. Listens and gets were answered in order
there too; this fake does not check their order.
fakeAsync((FakeAsync async) {
final FakeDatabaseServer server = FakeDatabaseServer();
final FirebaseDatabase db = databaseWithFakeServer(app, server);
db.ref('a').onValue.listen((DatabaseEvent e) => print(e.snapshot.value));
async.elapse(Duration.zero);
server.answerListen('a', 1); // prints 1
async.elapse(Duration.zero);
});
Constructors
- FakeDatabaseServer()
- Creates a server that accepts connections.
Properties
- acceptConnections ↔ bool
-
Whether a connection attempt succeeds. Default true. Set it to false, then call dropConnection, to
keep the client offline.
getter/setter pair
- connectionAttempts → int
-
The number of connection attempts the client has made.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
-
receivedAuthTokens
→ List<
String?> -
The auth token the client sent with each connection (and each token change while connected), oldest
first: null for a connection made without a user. A connection after revokeAuth sends a token
fetched with
forceRefresh: true.no setter -
requests
→ List<
String> -
The requests received, oldest first, as
'<action> <path>':qlisten,nunlisten,gget,pput,mmerge,oonDisconnect put, and so on, for example'q /a'. The path always has its leading slash. A listen, unlisten or get of a filtered query adds its wire query object as compact JSON with sorted keys:'q /a {"l":2,"vf":"l"}'.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- serverTime ↔ DateTime?
-
The server's clock, which resolves
ServerValue.timestampin acknowledged writes and onDisconnect writes; null (the default) readsDateTime.now().getter/setter pair
Methods
-
ackLastOnDisconnect(
{bool outOfOrder = false}) → void -
Acknowledges the most recent unanswered onDisconnect request. Ordered as ackLastWrite, with
the same
outOfOrder. -
ackLastWrite(
{bool outOfOrder = false}) → void -
Acknowledges the most recent unanswered write (put or merge): the written data, with server values
resolved (the timestamp from serverTime, an increment against the server's current value), is
pushed once to every open listen that overlaps it (with its tag for a filtered listen), then the write
is answered
ok, as the server does. -
ackNextOnDisconnect(
) → void - Acknowledges the oldest sent unanswered onDisconnect request. Throws a StateError when a write sent before it is unanswered, or when no onDisconnect request is sent and unanswered.
-
ackNextWrite(
) → void - Acknowledges the oldest sent unanswered write (put or merge), as ackLastWrite does. Throws a StateError when an onDisconnect request sent before it is unanswered, or when no write is sent and unanswered.
-
answerGet(
String path, Object? value) → void -
Answers the open get at
pathwithvalue. -
answerListen(
String path, Object? value, {Map< String, Object?> ? query}) → void -
Answers the open listen at
path('a/b'or'/a/b') withvalue(JSON,.priority/.valueallowed), completing it. -
cancelListen(
String path, String status, {Map< String, Object?> ? query}) → void -
Cancels the active listen at
pathwith the wirestatus, as a rules change would.queryselects the listen as for answerListen. -
dropConnection(
) → void - Drops the current connection; the client reconnects when acceptConnections allows it.
-
failGet(
String path, String status) → void -
Refuses the open get at
pathwith the wirestatus. -
failLastWrite(
String status, {bool outOfOrder = false}) → void -
Refuses the most recent unanswered write with the wire
status, for example'permission_denied'or'datastale'. Nothing is pushed. Ordered as ackLastWrite, with the sameoutOfOrder. -
failListen(
String path, String status, {Map< String, Object?> ? query}) → void -
Refuses the open listen at
pathwith the wirestatus, for example'permission_denied'.queryselects the listen as for answerListen. -
failNextWrite(
String status) → void -
Refuses the oldest sent unanswered write with the wire
status, as failLastWrite does. Throws as ackNextWrite does. -
kill(
String reason) → void -
Kills the connection with
reason, as the server's kill message does: the client stays offline until the instance is recreated. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
push(
String path, Object? value) → void -
Sends
valueas new data atpath, as another client's write would. -
revokeAuth(
) → void - Revokes the client's auth: the connection drops and the client listens again after reconnecting.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited