Sub topic 15
Testing and troubleshooting a subscriber
This chapter assumes you have a working subscriber like the one built in the earlier chapters. That means OAuth access tokens, the three metadata headers, GetSchema with Avro decoding, Subscribe with num_requested, and stored replay IDs. Here you will test the pieces and diagnose the failures you are most likely to hit.
Test the decoder without an org
Decoding does not need a network. Encode a record with the same schema, decode it, and compare. This catches schema and type mistakes early.
import io
import fastavro
SCHEMA = fastavro.parse_schema({
"type": "record",
"name": "Order_Event",
"fields": [
{"name": "OrderId", "type": "string"},
{"name": "Amount", "type": "double"},
],
})
def encode(record):
buf = io.BytesIO()
fastavro.schemaless_writer(buf, SCHEMA, record)
return buf.getvalue()
def decode(payload):
return fastavro.schemaless_reader(io.BytesIO(payload), SCHEMA)
def test_round_trip():
record = {"OrderId": "A-1", "Amount": 12.5}
assert decode(encode(record)) == record
test_round_trip()
print("decoder ok")Run it directly or with pytest. Keep the stream logic separate from decoding so you can test each part alone.
Test end to end with a real event
Use a sandbox or scratch org. Start your subscriber with LATEST, then publish a test event from another terminal. You can create a platform event record through the REST API, a Salesforce CLI command, or Apex. If your subscriber prints the decoded record and a replay ID, authentication, schema lookup, decoding, and flow control all work.
Read the gRPC error first
Most failures arrive as a grpc.RpcError. Log its status code and details before anything else.
import grpc
def run(stub, metadata, topic_name, replay_id=None):
try:
subscribe(stub, metadata, topic_name, replay_id)
except grpc.RpcError as err:
print("code:", err.code())
print("details:", err.details())
raiseA triage path
Common symptoms
| Symptom | Likely cause | Fix |
|---|---|---|
| `UNAUTHENTICATED` | Expired token, or `instanceurl` or `tenantid` does not match the token's org | Get a new token and rebuild the metadata |
| Error on resume with a stored replay ID | The ID is older than the retention window, or belongs to another topic | Fall back to `LATEST` or `EARLIEST` and accept the gap |
| Stream connects, then goes silent | You processed `num_requested` events and never sent another `FetchRequest` | Request more after each batch |
| Nothing arrives after start | `LATEST` only delivers events published after the stream opens | Publish a new event, or replay from a stored ID |
| Decode raises an exception | Wrong schema for the payload | Fetch the schema by the event's own `schema_id` |
Debugging habits
- Log the topic, replay ID, and
schema_idfor every event. - Log
latest_replay_idfrom responses that carry no events. It confirms the stream is alive. - Check that the topic name uses the right prefix, such as
/event/for platform events or/data/for change events. - Test reconnection on purpose by stopping the network or killing the process. Then confirm it resumes from the stored replay ID without duplicates or gaps.
Exercise
Your subscriber receives 10 events and then stops printing, with no error. The connection is still open. What is the most likely cause, and how do you fix it?
Show answer
The client set num_requested to 10 and never asked for more, so the server has nothing left to send. Send a new FetchRequest with num_requested set to the number of events just processed, after each batch.