func TestRealtimeChannel_Close(t *testing.T) {
	rec := ably.NewStateRecorder(8)
	app, client := testutil.ProvisionRealtime(nil, &ably.ClientOptions{Listener: rec.Channel()})
	defer safeclose(t, client, app)

	channel := client.Channels.Get("test")
	sub, err := channel.Subscribe()
	if err != nil {
		t.Fatalf("channel.Subscribe()=%v", err)
	}
	defer sub.Close()
	if err := wait(channel.Publish("hello", "world")); err != nil {
		t.Fatalf("channel.Publish()=%v", err)
	}
	done := make(chan error)
	go func() {
		msg, ok := <-sub.MessageChannel()
		if !ok {
			done <- errors.New("did not receive published message")
		}
		if msg.Name != "hello" || msg.Data != "world" {
			done <- fmt.Errorf(`want name="hello", data="world"; got %s, %s`, msg.Name, msg.Data)
		}
		if _, ok = <-sub.MessageChannel(); ok {
			done <- fmt.Errorf("expected channel to be closed")
		}
		done <- nil
	}()
	if err := channel.Close(); err != nil {
		t.Fatalf("channel.Close()=%v", err)
	}
	if err := client.Close(); err != nil {
		t.Fatalf("client.Close()=%v", err)
	}
	select {
	case err := <-done:
		if err != nil {
			t.Fatalf("waiting on subscribed channel close failed: err=%s", err)
		}
	case <-time.After(timeout):
		t.Fatalf("waiting on subscribed channel close timed out after %v", timeout)
	}
	rec.Stop()
	states := rec.States()
	for _, expected := range chanCloseTransitions {
		if reflect.DeepEqual(states, expected) {
			return
		}
	}
	t.Fatalf("unexpected state transitions %v", states)
}
func TestRealtimeConn_Connect(t *testing.T) {
	rec := ably.NewStateRecorder(4)
	app, client := testutil.ProvisionRealtime(nil, &ably.ClientOptions{Listener: rec.Channel()})
	defer safeclose(t, client, app)

	if err := await(client.Connection.State, ably.StateConnConnected); err != nil {
		t.Fatal(err)
	}
	if err := client.Close(); err != nil {
		t.Fatalf("client.Close()=%v", err)
	}
	rec.Stop()
	if states := rec.States(); !reflect.DeepEqual(states, connTransitions) {
		t.Errorf("expected states=%v; got %v", connTransitions, states)
	}
}