Skip to content

Latest commit

 

History

History
92 lines (54 loc) · 3.3 KB

File metadata and controls

92 lines (54 loc) · 3.3 KB

Chapter.13 Testing Channels and OTP

Testing the Information System

  • Cache being standalone GenServer, can be tested in isolation

  • add test helper function to assist clean shutdown of GenServer & handle errors like timeout

tests at cache_test.exs

Testing Nfo

  • add TestBackend stub, use it to check for results with items, empty, timeout & crash

Stubs wouldn't fail tests acting as checks, just providing replacement behavior.

Mocks replace real behavior as stub but also allow specifying expectation & results to be played at runtime.. failing a test if expectations don't meet.

Isolating Wolfram

  • dynamic mocks are avoided, replacement at global create issues for tests running concurrently

  • better way is to make difficult to live test code provider configurable, give replaceable testing implementation

making our HTTP service pluggable; with :httpc used in dev/prod & test client module for testing

# at wolfram.ex
  @http_client Application.get_env(:nfo, :wolfram)[:http_client] || :httpc
  ...
  |> @http_client.request()
  ...

# at test.ex config
config :nfo, :wolfram,
  app_id: "testx",
  http_client: Nfo.Test.HTTPClient

Adding Tests to Channels

  • underneath Channels are OTP servers; Phoenix provides Phoenix.ChannelTest module to simplify testing

apps/videologue_web/test/support/channel_case.ex was generated by Mix initially providing VideologueWeb.ChannelCase to be use/injected as foundation in channel tests

  • VideologueWeb just has one channel VideoChannel; before testing it test where channel process begins.. UserSocket module

Authenticating a Test Socket

test connect with signed token & fake tokens to work as desired

Communicating with a Test Channel

add a supprt script test_data.exs able to insert_user, insert_video & login

  • assert_reply ensures :ok/.. response; can pass a map to assert on specific fields

  • assert_broadcast make sure ur annotation were broadcast to subscribers

  • assert_reply & assert_broadcast are built on top of assert_receive

  • for new annotation triggers Nfo, add wolfram user as a backend-user will be used to broadcast reply by compute_additional_info

Wrapping Up

Chapter-14 What's Next


  • receiving error like
05:58:50.326 [error] Task #PID<0.682.0> started from VideologueWeb.Presence_shard0 terminating
** (stop) exited in: DBConnection.Holder.checkout(#PID<0.679.0>, [log: #Function<14.39275128/1 in Ecto.Adapters.SQL.with_log/3>, source: "users", timeout: 15000, pool_size: 10, pool: DBConnection.Ownership])
    ** (EXIT) shutdown: "owner #PID<0.678.0> exited"
    (db_connection 2.4.0) lib/db_connection/holder.ex:95: DBConnection.Holder.checkout/3
...

use Jose Valim's fix

or just mine test dummy implementation from Videologue Umbrella