defmodule Phoenix.Tranports.LongPollerTest do
  use ExUnit.Case, async: true
  use ConnHelper
  import ExUnit.CaptureIO
  alias Phoenix.Transports.LongPoller
  alias Phoenix.Tranports.LongPollerTest.Router

  def conn_with_session(session \\ %{}) do
    %Conn{private: %{plug_session: session}}
    |> put_private(:phoenix_router, Router)
  end

  @port 4809
  @window_ms 20
  @ensure_window_timeout_ms @window_ms * 3

  Application.put_env(:phoenix, Router, [
    https: false,
    http: [port: @port],
    secret_key_base: "7pe/JuPlX/rvpyk80h5r9eShTBtTLIY4WcDIX/r60Fz+8pnQDc1usobc9D7KvD9/l6DNZBXo5Uc8HXSpsuwCcA==",
    session: [store: :cookie, key: "_unit_test"],
    transports: [longpoller: [window_ms: @window_ms]]
  ])

  defmodule Router do
    use Phoenix.Router
    use Phoenix.Router.Socket, mount: "/ws"
  end

  setup_all do
    capture_io fn -> Router.start end
    on_exit &Router.stop/0
    :ok
  end


  test "start_session starts the LongPoller.Server and stores pid in session" do
    conn = conn_with_session
    assert LongPoller.longpoll_pid(conn) == :nopid
    {conn = %Conn{}, server_pid} = LongPoller.start_session(conn)

    assert Process.alive?(server_pid)
    {:ok, decoded_pid} = LongPoller.longpoll_pid(conn)
    assert server_pid == decoded_pid
  end

  test "longpoll_pid returns {:error, :terminated} if serialized pid is dead" do
    {conn = %Conn{}, server_pid} = LongPoller.start_session(conn_with_session)
    assert Process.alive?(server_pid)
    :timer.sleep @ensure_window_timeout_ms
    refute Process.alive?(server_pid)
    assert match? {:error, :terminated}, LongPoller.longpoll_pid(conn)
  end

  test "resume_session returns {:ok, conn, pid} if valid session" do
    {conn = %Conn{}, server_pid} = LongPoller.start_session(conn_with_session)
    assert match?({:ok, %Conn{}, ^server_pid}, LongPoller.resume_session(conn))
  end

  test "resume_session returns {:error, conn, :terminated} if dead session" do
    {conn = %Conn{}, _server_pid} = LongPoller.start_session(conn_with_session)
    :timer.sleep @ensure_window_timeout_ms
    assert match?({:error, %Conn{}, :terminated}, LongPoller.resume_session(conn))
  end

  test "resume_session returns {:error, conn, :terminated} if missing session" do
    conn = conn_with_session
    assert match?({:error, %Conn{}, :terminated}, LongPoller.resume_session(conn))
  end
end

Add LongPoller controller unit tests.
Add LongPoller controller unit tests.

evax/phoenix,ephe-meral/phoenix,Ben-Evolently/phoenix,VictorZeng/phoenix,jeffkreeftmeijer/phoenix,Kosmas/phoenix,keathley/phoenix,trarbr/phoenix,rodrigues/phoenix,javimolla/phoenix,ephe-meral/phoenix,gjaldon/phoenix,DavidAlphaFox/phoenix,mobileoverlord/phoenix,Gazler/phoenix,Joe-noh/phoenix,beni55/phoenix,paulcsmith/phoenix,jwarwick/phoenix,wsmoak/phoenix,jeffweiss/phoenix,daytonn/phoenix,nshafer/phoenix,Joe-noh/phoenix,michalmuskala/phoenix,optikfluffel/phoenix,rafbgarcia/phoenix,henrik/phoenix,wicliff/phoenix,sdnorm/phoenix,phoenixframework/phoenix,bitgamma/phoenix,Fluxuous/phoenix,ericmj/phoenix,mvidaurre/phoenix,JHKennedy4/phoenix,wsmoak/phoenix,korobkov/phoenix,doron2402/phoenix,kidaa/phoenix,manukall/phoenix,milafrerichs/phoenix,jxs/phoenix,michalmuskala/phoenix,nickgartmann/phoenix,yoavlt/phoenix,GabrielNicolasAvellaneda/phoenix,CultivateHQ/phoenix,iguberman/phoenix,Smulligan85/phoenix,jasond-s/phoenix,mitchellhenke/phoenix,Gazler/phoenix,vic/phoenix,andrewvy/phoenix,mvidaurre/phoenix,0x00evil/phoenix,doomspork/phoenix,ybur-yug/phoenix,mschae/phoenix,cas27/phoenix,seejee/phoenix,Dahoon/phoenix,jamilabreu/phoenix,ybur-yug/phoenix,mobileoverlord/phoenix,bsodmike/phoenix,mitchellhenke/phoenix,korobkov/phoenix,dmarkow/phoenix,elliotcm/phoenix,vic/phoenix,kholbekj/phoenix,kyledecot/phoenix,robomc/phoenix,justnoxx/phoenix,optikfluffel/phoenix,scrogson/phoenix,DavidAlphaFox/phoenix,tony612/phoenix,lancehalvorsen/phoenix,ahmetabdi/phoenix,eksperimental/phoenix,switchflip/phoenix,omotenko/phoenix_auth,linearregression/phoenix,romul/phoenix,seejee/phoenix,phoenixframework/phoenix,KazuCocoa/phoenix,nicrioux/phoenix,trarbr/phoenix,robhurring/phoenix,dmarkow/phoenix,arthurcolle/phoenix,evax/phoenix,lono-devices/phoenix,rafbgarcia/phoenix,CD1212/phoenix,thegrubbsian/phoenix,hsavit1/phoenix,ACPK/phoenix,ugisozols/phoenix,stevedomin/phoenix,esbullington/phoenix-1,omotenko/phoenix_auth,maxnordlund/phoenix,stevedomin/phoenix,ericmj/phoenix,CultivateHQ/phoenix,keathley/phoenix,xtity/simple_phoenix,manukall/phoenix,safi/phoenix,marcioj/phoenix,Rumel/phoenix,nshafer/phoenix,jamilabreu/phoenix,0x00evil/phoenix,jwarwick/phoenix,bdtomlin/phoenix,Rumel/phoenix,nshafer/phoenix,fbjork/phoenix,hotvulcan/phoenix,mschae/phoenix,cas27/phoenix,Eventacular/phoenix,Kosmas/phoenix,lancehalvorsen/phoenix,y-yagi/phoenix,begleynk/phoenix,foxnewsnetwork/phoenix,yonglehou/phoenix,iStefo/phoenix,FND/phoenix,bsmr-erlang/phoenix,sadiqmmm/phoenix,rafbgarcia/phoenix,lono-devices/phoenix,kevinold/phoenix,SmartCasual/phoenix,iStefo/phoenix,Smulligan85/phoenix,optikfluffel/phoenix,pap/phoenix,stefania11/phoenix,greyhwndz/phoenix,0x00evil/phoenix,bsmr-erlang/phoenix,rodrigues/phoenix,brucify/phoenix,pap/phoenix,StevenNunez/phoenix,gjaldon/phoenix,phoenixframework/phoenix,eksperimental/phoenix,andrewvy/phoenix