Skip to content

Commit

Permalink
Added ability to wait for correct shutdown by waiting for closed channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannes Georg committed Aug 8, 2014
1 parent 0417003 commit 097ea69
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions zk/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zk

import (
"testing"
"time"
)

func TestBasicCluster(t *testing.T) {
Expand Down Expand Up @@ -55,3 +56,39 @@ func TestClientClusterFailover(t *testing.T) {
t.Fatal("Wrong data for node 2")
}
}

func TestWaitForClose(t *testing.T) {
ts, err := StartTestCluster(1)
if err != nil {
t.Fatal(err)
}
defer ts.Stop()
zk, err := ts.Connect(0)
if err != nil {
t.Fatalf("Connect returned error: %+v", err)
}
timeout := time.After(30*time.Second)
CONNECTED:
for{
select{
case ev := <-zk.eventChan :
if ev.State == StateConnected {
break CONNECTED;
}
case <-timeout:
zk.Close()
t.Fatal("Timeout")
}
}
zk.Close()
for{
select{
case _,ok := <-zk.eventChan :
if !ok {
return
}
case <-timeout:
t.Fatal("Timeout")
}
}
}
1 change: 1 addition & 0 deletions zk/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func ConnectWithDialer(servers []string, recvTimeout time.Duration, dialer Diale
conn.loop()
conn.flushRequests(ErrClosing)
conn.invalidateWatches(ErrClosing)
close(conn.eventChan)
}()
return &conn, ec, nil
}
Expand Down

0 comments on commit 097ea69

Please sign in to comment.