   end
 
   test "#leave raise InvalidReturn exception when return type invalid" do
-    socket = %Socket{pid: self, router: Router, channel: "chan4", topic: "topic"}
-    sockets = HashDict.put(HashDict.new, {"chan4", "topic"}, socket)
-    message = %Message{channel: "chan4", topic: "topic", event: "join", message: %{}}
-
-    PubSub.create("chan4:topic")
-    {:ok, sockets} = Transport.dispatch(message, sockets, self, Router)
-    sock = HashDict.get(sockets, {"chan4", "topic"})
-    assert Socket.authorized?(sock, "chan4", "topic")
+    socket = new_socket
+    sockets = HashDict.put(HashDict.new, {"chan1", "topic"}, socket)
+    message = join_message({:ok, socket})
+
+    PubSub.create("chan1:topic")
+    {:ok, sockets} = Transport.dispatch(message, sockets, self, Router)
+    sock = HashDict.get(sockets, {"chan1", "topic"})
+    assert Socket.authorized?(sock, "chan1", "topic")
+    Process.put(:leave, :badreturn)
     assert_raise InvalidReturn, fn ->
       Transport.dispatch_leave(sockets, :reason)
     end
   end
 
   test "#event raises InvalidReturn exception when return type is invalid" do
-    socket = %Socket{pid: self, router: Router, channel: "chan5", topic: "topic"}
-    sockets = HashDict.put(HashDict.new, {"chan5", "topic"}, socket)
-    message = %Message{channel: "chan5", topic: "topic", event: "join", message: %{}}
-
-    PubSub.create("chan5:topic")
-    {:ok, sockets} = Transport.dispatch(message, sockets, self, Router)
-    sock = HashDict.get(sockets, {"chan5", "topic"})
-    assert Socket.authorized?(sock, "chan5", "topic")
-    message = %Message{channel: "chan5", topic: "topic", event: "boom", message: %{}}
+    socket = new_socket
+    sockets = HashDict.put(HashDict.new, {"chan1", "topic"}, socket)
+    message = join_message({:ok, socket})
+
+    PubSub.create("chan1:topic")
+    {:ok, sockets} = Transport.dispatch(message, sockets, self, Router)
+    sock = HashDict.get(sockets, {"chan1", "topic"})
+    assert Socket.authorized?(sock, "chan1", "topic")
+    message = %Message{channel: "chan1",
+                       topic: "topic",
+                       event: "boom",
+                       message: :badreturn}
 
     assert_raise InvalidReturn, fn ->
       Transport.dispatch(message, sockets, self, Router)

  end

  test "phoenix channel returns heartbeat message when received" do
    socket = %Socket{pid: self, router: Router9, channel: "phoenix"}
    sockets = HashDict.put(HashDict.new, {"phoenix", "conn"}, socket)
    message = %Message{channel: "phoenix", topic: "conn", event: "heartbeat", message: %{}}

    assert match?({:ok, _sockets}, Transport.dispatch(message, sockets, self, Router9))
    assert_received %Message{channel: "phoenix", topic: "conn", event: "heartbeat", message: %{}}
  end

  test "socket state can change when receiving regular process messages" do
    socket = %Socket{pid: self, router: Router, channel: "chan6", topic: "topic"}
    sockets = HashDict.put(HashDict.new, {"chan6", "topic"}, socket)
    message = %Message{channel: "chan6", topic: "topic", event: "join", message: %{}}

    PubSub.create("chan6:topic")
    {:ok, sockets} = Transport.dispatch(message, sockets, self, Router)
    {:ok, sockets} = Transport.dispatch_info(sockets, :stuff)
    socket = HashDict.get(sockets, {"chan6", "topic"})

    assert socket.assigns[:foo] == :bar
  end

  test "Socket state can be put and retrieved" do
    socket = %Socket{pid: self, router: Router, channel: "chan66"}
    socket = ChangeSocketStateChannel.event(socket, "put", %{val: 123})
    _socket = ChangeSocketStateChannel.event(socket, "get", %{"key" => :val})
    assert_received 123
  end
end

  end

  test "unsuccessful join denies socket access to channel/topic" do
    message = join_message({:error, new_socket, :unauthenticated})

    PubSub.create("chan1:topic")
    assert PubSub.subscribers("chan1:topic") == []
    {:error, sockets, :unauthenticated} = Transport.dispatch(message, HashDict.new, self, Router)
    refute HashDict.get(sockets, {"chan1", "topic"})
    refute PubSub.subscribers("chan1:topic") == [self]
  end

  test "#leave is called when the socket conn closes, and is unsubscribed" do
    socket = new_socket
    sockets = HashDict.put(HashDict.new, {"chan1", "topic"}, socket)
    message = join_message({:ok, socket})

    PubSub.create("chan1:topic")
    {:ok, sockets} = Transport.dispatch(message, sockets, self, Router)
    Process.put(:leave, socket)
    Transport.dispatch_leave(sockets, :reason)
    assert PubSub.subscribers("chan1:topic") == []
  end

  test "#info is called when receiving regular process messages" do
    socket = new_socket
    sockets = HashDict.put(HashDict.new, {"chan1", "topic"}, socket)
    message = join_message({:ok, socket})

    PubSub.create("chan1:topic")
    {:ok, sockets} = Transport.dispatch(message, sockets, self, Router)
    Transport.dispatch_info(sockets, socket)
    assert_received :info
  end

  test "#join raise InvalidReturn exception when return type invalid" do
    sockets = HashDict.put(HashDict.new, {"chan1", "topic"}, new_socket)
    message = join_message(:badreturn)

    assert_raise InvalidReturn, fn ->
      {:ok, _sockets} = Transport.dispatch(message, sockets, self, Router)
