2020-08-11 01:47:54 +02:00
|
|
|
# Real-time push and events
|
2017-02-10 10:09:31 +01:00
|
|
|
|
|
|
|
Zulip's "events system" is the server-to-client push system that
|
2021-08-20 21:53:28 +02:00
|
|
|
powers our real-time sync. This document explains how it works; to
|
2017-02-10 10:09:31 +01:00
|
|
|
read an example of how a complete feature using this system works,
|
|
|
|
check out the
|
2019-09-30 19:37:56 +02:00
|
|
|
[new application feature tutorial](../tutorials/new-feature-tutorial.md).
|
2017-02-10 10:09:31 +01:00
|
|
|
|
|
|
|
Any single-page web application like Zulip needs a story for how
|
|
|
|
changes made by one client are synced to other clients, though having
|
|
|
|
a good architecture for this is particularly important for a chat tool
|
2021-08-20 21:53:28 +02:00
|
|
|
like Zulip, since the state is constantly changing. When we talk
|
2017-02-10 10:09:31 +01:00
|
|
|
about clients, think a browser tab, mobile app, or API bot that needs
|
2021-08-20 21:53:28 +02:00
|
|
|
to receive updates to the Zulip data. The simplest example is a new
|
2017-02-10 10:09:31 +01:00
|
|
|
message being sent by one client; other clients must be notified in
|
2021-08-20 21:53:28 +02:00
|
|
|
order to display the message. But a complete application like Zulip
|
2017-02-10 10:09:31 +01:00
|
|
|
has dozens of different types of data that need to be synced to other
|
|
|
|
clients, whether it be new streams, changes in a user's name or
|
2021-08-20 21:53:28 +02:00
|
|
|
avatar, settings changes, etc. In Zulip, we call these updates that
|
2017-02-10 10:09:31 +01:00
|
|
|
need to be sent to other clients **events**.
|
|
|
|
|
|
|
|
An important thing to understand when designing such a system is that
|
|
|
|
events need to be synced to every client that has a copy of the old
|
|
|
|
data if one wants to avoid clients displaying inaccurate data to
|
2021-08-20 21:53:28 +02:00
|
|
|
users. So if a user has two browser windows open and sends a message,
|
2017-02-10 10:09:31 +01:00
|
|
|
every client controlled by that user as well as any recipients of the
|
|
|
|
message, including both of those two browser windows, will receive
|
2021-08-20 21:53:28 +02:00
|
|
|
that event. (Technically, we don't need to send events to the client
|
2017-02-10 10:09:31 +01:00
|
|
|
that triggered the change, but this approach saves a bunch of
|
|
|
|
unnecessary duplicate UI update code, since the client making the
|
|
|
|
change can just use the same code as every other client, maybe plus a
|
|
|
|
little notification that the operation succeeded).
|
|
|
|
|
|
|
|
Architecturally, there are a few things needed to make a successful
|
|
|
|
real-time sync system work:
|
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
- **Generation**. Generating events when changes happen to data, and
|
2017-02-10 10:09:31 +01:00
|
|
|
determining which users should receive each event.
|
2021-08-20 21:53:28 +02:00
|
|
|
- **Delivery**. Efficiently delivering those events to interested
|
2017-02-10 10:09:31 +01:00
|
|
|
clients, ideally in an exactly-once fashion.
|
2021-08-20 21:53:28 +02:00
|
|
|
- **UI updates**. Updating the UI in the client once it has received
|
2017-02-10 10:09:31 +01:00
|
|
|
events from the server.
|
|
|
|
|
|
|
|
Reactive JavaScript libraries like React and Vue can help simplify the
|
|
|
|
last piece, but there aren't good standard systems for doing
|
|
|
|
generation and delivery, so we have to build them ourselves.
|
|
|
|
|
|
|
|
This document discusses how Zulip solves the generation and delivery
|
|
|
|
problems in a scalable, correct, and predictable way.
|
|
|
|
|
|
|
|
## Generation system
|
|
|
|
|
|
|
|
Zulip's generation system is built around a Python function,
|
2021-08-20 21:53:28 +02:00
|
|
|
`send_event(realm, event, users)`. It accepts the realm (used for
|
2018-11-02 23:33:54 +01:00
|
|
|
sharding), the event data structure (just a Python dictionary with
|
|
|
|
some keys and value; `type` is always one of the keys but the rest
|
|
|
|
depends on the specific event) and a list of user IDs for the users
|
2021-08-20 21:53:28 +02:00
|
|
|
whose clients should receive the event. In special cases such as
|
2018-11-02 23:33:54 +01:00
|
|
|
message delivery, the list of users will instead be a list of dicts
|
|
|
|
mapping user IDs to user-specific data like whether that user was
|
2021-08-20 21:53:28 +02:00
|
|
|
mentioned in that message. The data passed to `send_event` are simply
|
2018-11-02 23:33:54 +01:00
|
|
|
marshalled as JSON and placed in the `notify_tornado` RabbitMQ queue
|
|
|
|
to be consumed by the delivery system.
|
2017-02-10 10:09:31 +01:00
|
|
|
|
|
|
|
Usually, this list of users is one of 3 things:
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- A single user (e.g. for user-level settings changes).
|
|
|
|
- Everyone in the realm (e.g. for organization-level settings changes,
|
2017-02-10 10:09:31 +01:00
|
|
|
like new realm emoji).
|
2021-08-20 21:45:39 +02:00
|
|
|
- Everyone who would receive a given message (for messages, emoji
|
2017-02-10 10:09:31 +01:00
|
|
|
reactions, message editing, etc.); i.e. the subscribers to a stream
|
2023-06-16 00:55:22 +02:00
|
|
|
or the people on a direct message thread.
|
2017-02-10 10:09:31 +01:00
|
|
|
|
|
|
|
It is the responsibility of the caller of `send_event` to choose the
|
2021-08-20 21:53:28 +02:00
|
|
|
list of user IDs correctly. There can be security problems if e.g. an
|
2023-06-16 00:55:22 +02:00
|
|
|
event containing direct message content is sent to the entire
|
2021-08-20 21:53:28 +02:00
|
|
|
organization. However, if an event isn't sent to enough clients,
|
2017-02-10 10:09:31 +01:00
|
|
|
there will likely be user-visible real-time sync bugs.
|
|
|
|
|
|
|
|
Most of the hard work in event generation is about defining consistent
|
|
|
|
event dictionaries that are clear, readable, will be useful to the
|
|
|
|
wide range of possible clients, and make it easy for developers.
|
|
|
|
|
|
|
|
## Delivery system
|
|
|
|
|
|
|
|
Zulip's event delivery (real-time push) system is based on Tornado,
|
2021-08-20 21:53:28 +02:00
|
|
|
which is ideal for handling a large number of open requests. Details
|
2017-02-10 10:09:31 +01:00
|
|
|
on Tornado are available in the
|
2019-09-30 19:37:56 +02:00
|
|
|
[architecture overview](../overview/architecture-overview.md), but in short it
|
2017-02-10 10:09:31 +01:00
|
|
|
is good at holding open a large number of connections for a long time.
|
2020-05-17 14:46:23 +02:00
|
|
|
The complete system is about 2000 lines of code in `zerver/tornado/`,
|
2017-02-10 10:09:31 +01:00
|
|
|
primarily `zerver/tornado/event_queue.py`.
|
|
|
|
|
|
|
|
Zulip's event delivery system is based on "long-polling"; basically
|
|
|
|
clients make `GET /json/events` calls to the server, and the server
|
|
|
|
doesn't respond to the request until it has an event to deliver to the
|
2021-08-20 21:53:28 +02:00
|
|
|
client. This approach is reasonably efficient and works everywhere
|
2017-02-10 10:09:31 +01:00
|
|
|
(unlike websockets, which have a decreasing but nonzero level of
|
|
|
|
client compatibility problems).
|
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
For each connected client, the **event queue server** maintains an
|
2017-02-10 10:09:31 +01:00
|
|
|
**event queue**, which contains any events that are to be delivered to
|
|
|
|
that client which have not yet been acknowledged by that client.
|
|
|
|
Ignoring the subtle details around error handling, the protocol is
|
|
|
|
pretty simple; when a client does a `GET /json/events` call, the
|
2021-08-20 21:53:28 +02:00
|
|
|
server checks if there are any events in the queue. If there are, it
|
|
|
|
returns the events immediately. If there aren't, it records that
|
2017-02-10 10:09:31 +01:00
|
|
|
queue as having a waiting client (often called a `handler` in the
|
|
|
|
code).
|
|
|
|
|
|
|
|
When it pulls an event off the `notify_tornado` RabbitMQ queue, it
|
|
|
|
simply delivers the event to each queue associated with one of the
|
2021-08-20 21:53:28 +02:00
|
|
|
target users. If the queue has a waiting client, it breaks the
|
2017-02-10 10:09:31 +01:00
|
|
|
long-poll connection by returning an HTTP response to the waiting
|
2021-08-20 21:53:28 +02:00
|
|
|
client request. If there is no waiting client, it simply pushes the
|
2017-02-10 10:09:31 +01:00
|
|
|
event onto the queue.
|
|
|
|
|
|
|
|
When starting up, each client makes a `POST /json/register` to the
|
2017-03-08 03:51:53 +01:00
|
|
|
server, which creates a new event queue for that client and returns the
|
2017-02-10 10:09:31 +01:00
|
|
|
`queue_id` as well as an initial `last_event_id` to the client (it can
|
|
|
|
also, optionally, fetch the initial data to save an RTT and avoid
|
|
|
|
races; see the below section on initial data fetches for details on
|
2021-08-20 21:53:28 +02:00
|
|
|
why this is useful). Once the event queue is registered, the client
|
2017-02-10 10:09:31 +01:00
|
|
|
can just do an infinite loop calling `GET /json/events` with those
|
|
|
|
parameters, updating `last_event_id` each time to acknowledge any
|
|
|
|
events it has received (see `call_on_each_event` in the
|
|
|
|
[Zulip Python API bindings][api-bindings-code] for a complete example
|
2021-08-20 21:53:28 +02:00
|
|
|
implementation). When handling each `GET /json/events` request, the
|
2017-11-29 08:20:40 +01:00
|
|
|
queue server can safely delete any events that have an event ID less
|
2017-02-10 10:09:31 +01:00
|
|
|
than or equal to the client's `last_event_id` (event IDs are just a
|
|
|
|
counter for the events a given queue has received.)
|
|
|
|
|
|
|
|
If network failures were impossible, the `last_event_id` parameter in
|
|
|
|
the protocol would not be required, but it is important for enabling
|
2021-08-20 21:53:28 +02:00
|
|
|
exactly-once delivery in the presence of potential failures. (Without
|
2017-02-10 10:09:31 +01:00
|
|
|
it, the queue server would have to delete events from the queue as
|
|
|
|
soon as it attempted to send them to the client; if that specific HTTP
|
|
|
|
response didn't reach the client due to a network TCP failure, then
|
|
|
|
those events could be lost).
|
|
|
|
|
2021-08-31 23:32:37 +02:00
|
|
|
[api-bindings-code]: https://github.com/zulip/python-zulip-api/blob/main/zulip/zulip/__init__.py
|
2017-02-10 10:09:31 +01:00
|
|
|
|
|
|
|
The queue servers are a very high-traffic system, processing at a
|
2017-11-29 08:20:40 +01:00
|
|
|
minimum one request for every message delivered to every Zulip client.
|
2017-02-10 10:09:31 +01:00
|
|
|
Additionally, as a workaround for low-quality NAT servers that kill
|
|
|
|
HTTP connections that are open without activity for more than 60s, the
|
|
|
|
queue servers also send a heartbeat event to each queue at least once
|
|
|
|
every 45s or so (if no other events have arrived in the meantime).
|
|
|
|
|
|
|
|
To avoid a large memory and other resource leak, the queues are
|
|
|
|
garbage collected after (by default) 10 minutes of inactivity from a
|
|
|
|
client, under the theory that the client has likely gone off the
|
2021-08-20 21:53:28 +02:00
|
|
|
Internet (or no longer exists) access; this happens constantly. If
|
2017-02-10 10:09:31 +01:00
|
|
|
the client returns, it will receive a "queue not found" error when
|
2020-10-12 21:10:50 +02:00
|
|
|
requesting events; its handler for this case should just restart the
|
2017-02-10 10:09:31 +01:00
|
|
|
client / reload the browser so that it refetches initial data the same
|
2021-08-20 21:53:28 +02:00
|
|
|
way it would on startup. Since clients have to implement their
|
2017-02-10 10:09:31 +01:00
|
|
|
startup process anyway, this approach adds minimal technical
|
2021-08-20 21:53:28 +02:00
|
|
|
complexity to clients. A nice side effect is that if the event queue
|
2021-05-10 07:02:14 +02:00
|
|
|
server (which stores queues in memory) were to crash and lose
|
2017-02-10 10:09:31 +01:00
|
|
|
its data, clients would recover, just as if they had lost Internet
|
|
|
|
access briefly (there is some DoS risk to manage, though).
|
|
|
|
|
2020-12-19 03:05:20 +01:00
|
|
|
Note that the garbage-collection system has hooks that are important
|
2022-02-24 00:17:21 +01:00
|
|
|
for the implementation of [notifications](notifications.md).
|
2020-12-19 03:05:20 +01:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
(The event queue server is designed to save any event queues to disk
|
2017-02-10 10:09:31 +01:00
|
|
|
and reload them when the server is restarted, and catches exceptions
|
|
|
|
carefully, so such incidents are very rare, but it's nice to have a
|
|
|
|
design that handles them without leaving broken out-of-date clients
|
|
|
|
anyway).
|
|
|
|
|
|
|
|
## The initial data fetch
|
|
|
|
|
|
|
|
When a client starts up, it usually wants to get 2 things from the
|
|
|
|
server:
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- The "current state" of various pieces of data, e.g. the current
|
2017-02-10 10:09:31 +01:00
|
|
|
settings, set of users in the organization (for typeahead), stream,
|
|
|
|
messages, etc. (aka the "initial state").
|
2021-08-20 21:45:39 +02:00
|
|
|
- A subscription to receive updates to those data when they are
|
2017-02-10 10:09:31 +01:00
|
|
|
changed by a client (aka an event queue).
|
|
|
|
|
|
|
|
Ideally, one would get those two things atomically, i.e. if some other
|
2017-08-15 05:21:08 +02:00
|
|
|
user changes their name, either the name change happens after the
|
2017-02-10 10:09:31 +01:00
|
|
|
fetch (and thus the old name is in the initial state and there will be
|
2017-08-15 05:21:08 +02:00
|
|
|
an event in the queue for the name change) or before (the new name is
|
2017-02-10 10:09:31 +01:00
|
|
|
in the initial state, and there is no event for that name change in
|
|
|
|
the queue).
|
|
|
|
|
|
|
|
Achieving this atomicity goals means we save a huge amount of work
|
|
|
|
that the N clients for Zulip don't need to worry about a wide range of
|
|
|
|
potential rare and hard to reproduce race conditions; we just have to
|
|
|
|
implement things correctly once in the Zulip server.
|
|
|
|
|
|
|
|
This is quite challenging to do technically, because fetching the
|
|
|
|
initial state for a complex web application like Zulip might involve
|
|
|
|
dozens of queries to the database, caches, etc. over the course of
|
|
|
|
100ms or more, and it is thus nearly impossible to do all of those
|
2021-08-20 21:53:28 +02:00
|
|
|
things together atomically. So instead, we use a more complicated
|
2017-02-10 10:09:31 +01:00
|
|
|
algorithm that can produce the atomic result from non-atomic
|
2021-08-20 21:53:28 +02:00
|
|
|
subroutines. Here's how it works when you make a `register` API
|
2017-02-10 23:04:46 +01:00
|
|
|
request; the logic is in `zerver/views/events_register.py` and
|
2021-08-20 21:53:28 +02:00
|
|
|
`zerver/lib/events.py`. The request is directly handled by Django:
|
2017-02-10 10:09:31 +01:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Django makes an HTTP request to Tornado, requesting that a new event
|
2017-02-10 10:09:31 +01:00
|
|
|
queue be created, and records its queue ID.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Django does all the various database/cache/etc. queries to fetch the
|
2017-02-10 10:09:31 +01:00
|
|
|
data, non-atomically, from the various data sources (see
|
|
|
|
the `fetch_initial_state_data` function).
|
2021-08-20 21:45:39 +02:00
|
|
|
- Django makes a second HTTP request to Tornado, requesting any events
|
2017-02-10 10:09:31 +01:00
|
|
|
that had been added to the Tornado event queue since it
|
|
|
|
was created.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Finally, Django "applies" the events (see the `apply_events`
|
2021-08-20 21:53:28 +02:00
|
|
|
function) to the initial state that it fetched. E.g. for a name
|
2017-02-10 10:09:31 +01:00
|
|
|
change event, it finds the user data in the `realm_user` data
|
2020-03-17 13:57:10 +01:00
|
|
|
structure, and updates it to have the new name.
|
2017-02-10 10:09:31 +01:00
|
|
|
|
2019-03-01 18:21:31 +01:00
|
|
|
### Testing
|
|
|
|
|
2020-09-28 19:23:24 +02:00
|
|
|
The design above achieves everything we desire, at the cost that we need to
|
2021-08-20 21:53:28 +02:00
|
|
|
write a correct `apply_events` function. This is a difficult function to
|
2020-09-28 19:23:24 +02:00
|
|
|
implement correctly, because the situations that it handles almost never
|
2021-08-20 21:53:28 +02:00
|
|
|
happen (being race conditions) during manual testing. Fortunately, we have
|
2020-09-28 19:23:24 +02:00
|
|
|
a protocol for testing `apply_events` in our automated backend tests.
|
|
|
|
|
|
|
|
#### Overview
|
|
|
|
|
|
|
|
Once you are completely confident that an "action function" works correctly
|
|
|
|
in terms of "normal" operation (which typically involves writing a
|
|
|
|
full-stack test for the corresponding POST/GET operation), then you will be
|
|
|
|
ready to write a test in `test_events.py`.
|
|
|
|
|
|
|
|
The actual code for a `test_events` test can be quite concise:
|
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```python
|
|
|
|
def test_default_streams_events(self) -> None:
|
|
|
|
stream = get_stream("Scotland", self.user_profile.realm)
|
|
|
|
events = self.verify_action(lambda: do_add_default_stream(stream))
|
|
|
|
check_default_streams("events[0]", events[0])
|
|
|
|
# (some details omitted)
|
|
|
|
```
|
2020-09-28 19:23:24 +02:00
|
|
|
|
|
|
|
The real trick is debugging these tests.
|
|
|
|
|
|
|
|
The test example above has three things going on:
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Set up some data (`get_stream`)
|
|
|
|
- Call `verify_action` with an action function (`do_add_default_stream`)
|
|
|
|
- Use a schema checker to validate data (`check_default_streams`)
|
2020-09-28 19:23:24 +02:00
|
|
|
|
|
|
|
#### verify_action
|
|
|
|
|
|
|
|
All the heavy lifting that pertains to `apply_events` happens within the
|
|
|
|
call to `verify_action`, which is a test helper in the `BaseAction` class
|
|
|
|
within `test_events.py`.
|
|
|
|
|
2020-09-28 21:35:55 +02:00
|
|
|
The `verify_action` function simulates the possible race condition in
|
2020-09-28 19:23:24 +02:00
|
|
|
order to verify that the `apply_events` logic works correctly in the
|
2021-08-20 21:53:28 +02:00
|
|
|
context of some action function. To use our concrete example above,
|
2020-09-28 21:35:55 +02:00
|
|
|
we are seeing that applying the events from the
|
|
|
|
`do_remove_default_stream` action inside of `apply_events` to a stale
|
|
|
|
copy of your state results in the same state dictionary as doing the
|
|
|
|
action and then fetching a fresh copy of the state.
|
2020-09-28 19:23:24 +02:00
|
|
|
|
|
|
|
In particular, `verify_action` does the following:
|
2017-02-10 10:09:31 +01:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Call `fetch_initial_state_data` to get the current state.
|
|
|
|
- Call the action function (e.g. `do_add_default_stream`).
|
|
|
|
- Capture the events generated by the action function.
|
|
|
|
- Check the events generated are documented in the [OpenAPI
|
2020-09-28 21:35:55 +02:00
|
|
|
schema](../documentation/api.md) defined in
|
|
|
|
`zerver/openapi/zulip.yaml`.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Call `apply_events(state, events)`, to get the resulting "hybrid state".
|
|
|
|
- Call `fetch_initial_state_data` again to get the "normal state".
|
|
|
|
- Compare the two results.
|
2020-09-28 19:23:24 +02:00
|
|
|
|
2020-09-28 21:35:55 +02:00
|
|
|
In the event that you wrote the `apply_events` logic correctly the
|
|
|
|
first time, then the two states will be identical, and the
|
|
|
|
`verify_action` call will succeed and return the events that came from
|
|
|
|
the action.
|
2020-09-28 19:23:24 +02:00
|
|
|
|
|
|
|
Often you will get the `apply_events` logic wrong at first, which will
|
|
|
|
cause `verify_action` to fail. To help you debug, it will print a diff
|
2019-03-01 18:21:31 +01:00
|
|
|
between the "hybrid state" and the "normal state" obtained from
|
2020-09-28 19:23:24 +02:00
|
|
|
calling `fetch_initial_state_data` after the changes. If you encounter a
|
|
|
|
diff like this, you may be in for a challenging debugging exercise. It
|
|
|
|
will be helpful to re-read this documentation to understand the rationale
|
|
|
|
behind the `apply_events` function. It may also be helpful to read the code
|
|
|
|
for `verify_action` itself. Finally, you may want to ask for help on chat.
|
|
|
|
|
|
|
|
Before we move on to the next step, it's worth noting that `verify_action`
|
2021-08-20 21:53:28 +02:00
|
|
|
only has one required parameter, which is the action function. We
|
2020-09-28 19:23:24 +02:00
|
|
|
typically express the action function as a lambda, so that we
|
|
|
|
can pass in arguments:
|
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```python
|
|
|
|
events = self.verify_action(lambda: do_add_default_stream(stream))
|
|
|
|
```
|
2020-09-28 19:23:24 +02:00
|
|
|
|
|
|
|
There are some notable optional parameters for `verify_action`:
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- `state_change_expected` must be set to `False` if your action
|
2021-08-20 07:09:04 +02:00
|
|
|
doesn't actually require state changes for some reason; otherwise,
|
|
|
|
`verify_action` will complain that your test doesn't really
|
2021-08-20 21:53:28 +02:00
|
|
|
exercise any `apply_events` logic. Typing notifications (which
|
2022-02-08 00:13:33 +01:00
|
|
|
are ephemeral) are a common place where we use this.
|
2020-09-28 19:23:24 +02:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- `num_events` will tell `verify_action` how many events the
|
2021-08-20 07:09:04 +02:00
|
|
|
`hamlet` user will receive after the action (the default is 1).
|
2020-09-28 19:23:24 +02:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- parameters such as `client_gravatar` and `slim_presence` get
|
2021-08-20 07:09:04 +02:00
|
|
|
passed along to `fetch_initial_state_data` (and it's important
|
|
|
|
to test both boolean values of these parameters for relevant
|
|
|
|
actions).
|
2020-09-28 19:23:24 +02:00
|
|
|
|
2020-09-28 21:35:55 +02:00
|
|
|
For advanced use cases of `verify_action`, we highly recommend reading
|
2020-09-28 19:23:24 +02:00
|
|
|
the code itself in `BaseAction` (in `test_events.py`).
|
|
|
|
|
|
|
|
#### Schema checking
|
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
The `test_events.py` system has two forms of schema checking. The
|
2020-09-28 21:35:55 +02:00
|
|
|
first is verifying that you've updated the [GET /events API
|
|
|
|
documentation](https://zulip.com/api/get-events) to document your new
|
|
|
|
event's format for benefit of the developers of Zulip's mobile app,
|
2021-08-20 21:53:28 +02:00
|
|
|
terminal app, and other API clients. See the [API documentation
|
2020-09-28 21:35:55 +02:00
|
|
|
docs](../documentation/api.md) for details on the OpenAPI
|
|
|
|
documentation.
|
|
|
|
|
|
|
|
The second is higher-detail check inside `test_events` that this
|
2021-08-20 21:53:28 +02:00
|
|
|
specific test generated the expected series of events. Let's look at
|
2020-09-28 21:35:55 +02:00
|
|
|
the last line of our example test snippet:
|
2020-09-28 19:23:24 +02:00
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```python
|
|
|
|
# ...
|
|
|
|
events = self.verify_action(lambda: do_add_default_stream(stream))
|
|
|
|
check_default_streams("events[0]", events[0])
|
|
|
|
```
|
2020-09-28 19:23:24 +02:00
|
|
|
|
|
|
|
We have discussed `verify_action` in some detail, and you will
|
|
|
|
note that it returns the actual events generated by the action
|
2021-08-20 21:53:28 +02:00
|
|
|
function. It is part of our test discipline in `test_events` to
|
2020-09-28 19:23:24 +02:00
|
|
|
verify that the events are formatted in a predictable way.
|
|
|
|
|
|
|
|
Ideally, we would test that events match the exact data that we
|
|
|
|
expect, but it can be difficult to do this due to unpredictable
|
2021-08-20 21:53:28 +02:00
|
|
|
things like database ids. So instead, we just verify the "schema"
|
|
|
|
of the event(s). We use a schema checker like `check_default_streams`
|
2020-09-28 19:23:24 +02:00
|
|
|
to validate the types of the data.
|
|
|
|
|
|
|
|
If you are creating a new event format, then you will have to
|
2021-08-20 21:53:28 +02:00
|
|
|
write your own schema checker in `event_schema.py`. Here is
|
2020-09-28 19:23:24 +02:00
|
|
|
the example relevant to our example:
|
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```python
|
|
|
|
default_streams_event = event_dict_type(
|
|
|
|
required_keys=[
|
|
|
|
("type", Equals("default_streams")),
|
|
|
|
("default_streams", ListType(DictType(basic_stream_fields))),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
check_default_streams = make_checker(default_streams_event)
|
|
|
|
```
|
2020-09-28 19:23:24 +02:00
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
Note that `basic_stream_fields` is not shown in these docs. The
|
2020-09-28 19:23:24 +02:00
|
|
|
best way to understand how to write schema checkers is to read
|
2021-08-20 21:53:28 +02:00
|
|
|
`event_schema.py`. There is a large block comment at the top of
|
2020-09-28 19:23:24 +02:00
|
|
|
the file, and then you can skim the rest of the file to see the
|
|
|
|
patterns.
|
|
|
|
|
2020-09-28 21:35:55 +02:00
|
|
|
When you create a new schema checker for a new event, you not only
|
|
|
|
make the `test_events` test more rigorous, you also allow our other
|
|
|
|
tools to use the same schema checker to validate event formats in our
|
|
|
|
node test fixtures and our OpenAPI documentation.
|
|
|
|
|
|
|
|
#### Node testing
|
|
|
|
|
|
|
|
Once you've completed backend testing, be sure to add an example event
|
2023-02-22 23:04:10 +01:00
|
|
|
in `web/tests/lib/events.js`, a test of the
|
2020-09-28 21:35:55 +02:00
|
|
|
`server_events_dispatch.js` code for that event in
|
2023-02-22 23:04:10 +01:00
|
|
|
`web/tests/dispatch.test.js`, and verify your example
|
2020-09-28 21:35:55 +02:00
|
|
|
against the two versions of the schema that you declared above using
|
2021-01-20 19:39:32 +01:00
|
|
|
`tools/check-schemas`.
|
2020-09-28 19:23:24 +02:00
|
|
|
|
|
|
|
#### Code coverage
|
2017-02-10 10:09:31 +01:00
|
|
|
|
|
|
|
The final detail we need to ensure that `apply_events` always works
|
2020-06-27 17:03:37 +02:00
|
|
|
correctly is to make sure that we have relevant tests for
|
2021-08-20 21:53:28 +02:00
|
|
|
every event type that can be generated by Zulip. This can be tested
|
2020-06-27 17:03:37 +02:00
|
|
|
manually using `test-backend --coverage BaseAction` and then
|
2021-08-20 21:53:28 +02:00
|
|
|
checking that all the calls to `send_event` are covered. Someday
|
2017-02-10 10:09:31 +01:00
|
|
|
we'll add automation that verifies this directly by inspecting the
|
|
|
|
coverage data.
|
|
|
|
|
2020-09-28 19:23:24 +02:00
|
|
|
#### page_params
|
|
|
|
|
2021-05-14 00:16:30 +02:00
|
|
|
In the Zulip web app, the data returned by the `register` API is
|
2017-05-14 07:49:35 +02:00
|
|
|
available via the `page_params` parameter.
|
|
|
|
|
2017-02-10 10:09:31 +01:00
|
|
|
### Messages
|
|
|
|
|
|
|
|
One exception to the protocol described in the last section is the
|
2021-08-20 21:53:28 +02:00
|
|
|
actual messages. Because Zulip clients usually fetch them in a
|
2017-02-10 10:09:31 +01:00
|
|
|
separate AJAX call after the rest of the site is loaded, we don't need
|
2021-08-20 21:53:28 +02:00
|
|
|
them to be included in the initial state data. To handle those
|
2017-02-10 10:09:31 +01:00
|
|
|
correctly, clients are responsible for discarding events related to
|
|
|
|
messages that the client has not yet fetched.
|
2018-11-30 00:48:13 +01:00
|
|
|
|
|
|
|
Additionally, see
|
2022-02-24 00:17:21 +01:00
|
|
|
[the main documentation on sending messages](sending-messages.md).
|
2021-05-24 22:46:26 +02:00
|
|
|
|
|
|
|
## Schema changes
|
|
|
|
|
|
|
|
When changing the format of events sent into Tornado, it's important
|
|
|
|
to make sure we handle backwards-compatibility properly.
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- If we're adding a new event type or new fields to an existing event
|
2021-05-24 22:46:26 +02:00
|
|
|
type, we just need to carefully document the changes in the [API
|
|
|
|
documentation](../documentation/api.md), being careful to bump
|
|
|
|
`API_FEATURE_LEVEL` and include a `**Changes**` entry in the updated
|
2021-08-20 21:53:28 +02:00
|
|
|
`GET /events` API documentation. It's also a good idea to and open
|
2021-05-24 22:46:26 +02:00
|
|
|
issues with the mobile and terminal projects to notify them.
|
2021-08-20 21:45:39 +02:00
|
|
|
- If we're making changes that could confuse existing client app logic
|
2021-05-24 22:46:26 +02:00
|
|
|
that parses events (E.g. changing the type/meaning of an existing
|
|
|
|
field, or removing a field), we need to be very careful, since Zulip
|
2021-08-20 21:53:28 +02:00
|
|
|
supports old clients connecting to a modern server. See our
|
2021-05-24 22:46:26 +02:00
|
|
|
[release lifecycle](../overview/release-lifecycle.md) documentation
|
2021-08-20 21:53:28 +02:00
|
|
|
for more details on the policy. Our technical solution is to add a
|
2021-05-24 22:46:26 +02:00
|
|
|
`client_capabilities` flag for the new format and have the code
|
|
|
|
continue sending data in the old format for clients that don't
|
2021-08-20 21:53:28 +02:00
|
|
|
declare support for the new capability. `bulk_message_deletion` is
|
|
|
|
a good example to crib from. (A few years later, we'll make the
|
2021-05-24 22:46:26 +02:00
|
|
|
client capability required and remove support for not having it).
|
2021-08-20 21:45:39 +02:00
|
|
|
- For most event types, Tornado just passes the event through
|
2021-05-24 22:46:26 +02:00
|
|
|
transparently, and `event_queue.py` requires no changes.
|
2021-08-20 21:45:39 +02:00
|
|
|
- However, when changing the format of data used by Tornado code, like
|
2021-05-24 22:46:26 +02:00
|
|
|
renaming the `presence_idle_user_ids` field in `message` events, we
|
|
|
|
need to be careful, because pre-upgrade events may be present in
|
2021-08-20 21:53:28 +02:00
|
|
|
Tornado's queue when we upgrade Tornado. So it's essential to write
|
2021-05-24 22:46:26 +02:00
|
|
|
logic in `event_queue.py` to translate the old format into the new
|
|
|
|
format, or Tornado may crash when upgrading past the relevant
|
|
|
|
commit. We attempt to contain that sort of logic in the `from_dict`
|
|
|
|
function (which is used for changing event queue formats) and
|
|
|
|
`client_capabilities` conditionals (E.g. in
|
2021-05-27 00:21:48 +02:00
|
|
|
`process_deletion_event`). Compatibility code not related to a
|
|
|
|
`client_capabilities` entry should be marked with a
|
|
|
|
`# TODO/compatibility: ...` comment noting when it can be safely deleted;
|
|
|
|
we grep for these comments entries during major releases.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Schema changes are a sensitive operation, and like with database
|
2021-05-24 22:46:26 +02:00
|
|
|
schema changes, it's critical to do thoughtful manual testing.
|
|
|
|
E.g. run the mobile app against your test server and verify it
|
|
|
|
handles the new event properly, or arrange for your new Tornado code
|
|
|
|
to actually process a pre-upgrade event and verify via the browser
|
|
|
|
console what came out.
|