ExGram.Updates.Test (ex_gram v0.65.0)

Copy Markdown View Source

Updates implementation for testing purposes.

This GenServer implements the updates interface for test environments. Instead of fetching updates from Telegram, it provides push_update/2 to inject updates directly into the bot's dispatcher.

The actual synchrony of handler execution depends on the bot's handler_mode:

  • :sync (default when using ExGram.Test.start_bot/3) - the handler runs inline within the dispatcher's process; push_update/2 blocks until it completes.
  • :async (production default) - the handler is spawned; push_update/2 returns before the handler runs.

Configured with config :ex_gram, updates: ExGram.Updates.Test.

See the Testing guide for more details.

Summary

Functions

Returns a specification to start this module under a supervisor.

Callback implementation for GenServer.init/1.

Push an update through the bot's ExGram.Dispatcher pipeline.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

init(arg)

Callback implementation for GenServer.init/1.

push_update(bot_name, update)

Push an update through the bot's ExGram.Dispatcher pipeline.

The update is delivered via GenServer.call/2, so the dispatcher always receives it synchronously. Whether this call blocks until the handler finishes depends on the bot's handler_mode:

  • :sync (default from ExGram.Test.start_bot/3) - the handler runs inline inside the dispatcher's handle_call, so this function returns only after the full pipeline has executed and all API calls have been made.
  • :async - the handler is spawned in a separate process; this function returns as soon as the dispatcher has enqueued the update.

Process isolation is handled automatically by ExGram.Test.start_bot/3, which allows the Dispatcher and Updates worker to use the test's stubs from startup. No manual allow/2 call is needed before calling this function.

Parameters

  • bot_name - The bot's registered name (atom), which is also the Dispatcher's name
  • update - An ExGram.Model.Update.t/0 struct

Example

update = %ExGram.Model.Update{
  update_id: 1,
  message: %ExGram.Model.Message{...}
}

# With handler_mode: :sync (default), the handler has completed when this returns
ExGram.Updates.Test.push_update(:my_bot, update)

start_link(opts)