Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(529)

Side by Side Diff: suite_test.go

Issue 5699093: zookeeper: make Conn.Close safe to call concurrently with other operations.
Patch Set: zookeeper: make Conn.Close safe to call concurrently with other operations. Created 12 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « retry_test.go ('k') | zk.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 package zookeeper_test 1 package zookeeper_test
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 . "launchpad.net/gocheck" 5 . "launchpad.net/gocheck"
6 zk "launchpad.net/gozk/zookeeper" 6 zk "launchpad.net/gozk/zookeeper"
7 "os" 7 "os"
8 "testing" 8 "testing"
9 "time" 9 "time"
10 ) 10 )
(...skipping 13 matching lines...) Expand all
24 24
25 handles []*zk.Conn 25 handles []*zk.Conn
26 events []*zk.Event 26 events []*zk.Event
27 liveWatches int 27 liveWatches int
28 deadWatches chan bool 28 deadWatches chan bool
29 } 29 }
30 30
31 var logLevel = 0 //zk.LOG_ERROR 31 var logLevel = 0 //zk.LOG_ERROR
32 32
33 func (s *S) init(c *C) (*zk.Conn, chan zk.Event) { 33 func (s *S) init(c *C) (*zk.Conn, chan zk.Event) {
34 c.Logf("init dialling %q", s.zkAddr)
34 conn, watch, err := zk.Dial(s.zkAddr, 5e9) 35 conn, watch, err := zk.Dial(s.zkAddr, 5e9)
35 c.Assert(err, IsNil) 36 c.Assert(err, IsNil)
36 s.handles = append(s.handles, conn) 37 s.handles = append(s.handles, conn)
37 bufferedWatch := make(chan zk.Event, 256) 38 bufferedWatch := make(chan zk.Event, 256)
38 39
39 select { 40 select {
40 case e, ok := <-watch: 41 case e, ok := <-watch:
41 c.Assert(ok, Equals, true) 42 c.Assert(ok, Equals, true)
42 c.Assert(e.Type, Equals, zk.EVENT_SESSION) 43 c.Assert(e.Type, Equals, zk.EVENT_SESSION)
43 c.Assert(e.State, Equals, zk.STATE_CONNECTED) 44 c.Assert(e.State, Equals, zk.STATE_CONNECTED)
(...skipping 20 matching lines...) Expand all
64 } 65 }
65 } 66 }
66 s.deadWatches <- true 67 s.deadWatches <- true
67 }() 68 }()
68 69
69 return conn, bufferedWatch 70 return conn, bufferedWatch
70 } 71 }
71 72
72 func (s *S) SetUpTest(c *C) { 73 func (s *S) SetUpTest(c *C) {
73 c.Assert(zk.CountPendingWatches(), Equals, 0, 74 c.Assert(zk.CountPendingWatches(), Equals, 0,
74 » » Bug("Test got a dirty watch state before running!")) 75 » » Commentf("Test got a dirty watch state before running!"))
75 zk.SetLogLevel(logLevel) 76 zk.SetLogLevel(logLevel)
76 } 77 }
77 78
78 func (s *S) TearDownTest(c *C) { 79 func (s *S) TearDownTest(c *C) {
79 // Close all handles opened in s.init(). 80 // Close all handles opened in s.init().
80 for _, handle := range s.handles { 81 for _, handle := range s.handles {
81 handle.Close() 82 handle.Close()
82 } 83 }
83 84
84 // Wait for all the goroutines created in s.init() to terminate. 85 // Wait for all the goroutines created in s.init() to terminate.
85 for s.liveWatches > 0 { 86 for s.liveWatches > 0 {
86 select { 87 select {
87 case <-s.deadWatches: 88 case <-s.deadWatches:
88 s.liveWatches -= 1 89 s.liveWatches -= 1
89 case <-time.After(5e9): 90 case <-time.After(5e9):
90 panic("There's a locked watch goroutine :-(") 91 panic("There's a locked watch goroutine :-(")
91 } 92 }
92 } 93 }
93 94
94 // Reset the list of handles. 95 // Reset the list of handles.
95 s.handles = make([]*zk.Conn, 0) 96 s.handles = make([]*zk.Conn, 0)
96 97
97 c.Assert(zk.CountPendingWatches(), Equals, 0, 98 c.Assert(zk.CountPendingWatches(), Equals, 0,
98 » » Bug("Test left live watches behind!")) 99 » » Commentf("Test left live watches behind!"))
99 } 100 }
100 101
101 // We use the suite set up and tear down to manage a custom ZooKeeper 102 // We use the suite set up and tear down to manage a custom ZooKeeper
102 // 103 //
103 func (s *S) SetUpSuite(c *C) { 104 func (s *S) SetUpSuite(c *C) {
104 var err error 105 var err error
105 s.deadWatches = make(chan bool) 106 s.deadWatches = make(chan bool)
106 107
107 » // N.B. We meed to create a subdirectory because zk.CreateServer 108 » // N.B. We need to create a subdirectory because zk.CreateServer
108 // insists on creating its own directory. 109 // insists on creating its own directory.
109 110
110 s.zkTestRoot = c.MkDir() + "/zk" 111 s.zkTestRoot = c.MkDir() + "/zk"
111 port := 21812 112 port := 21812
112 s.zkAddr = fmt.Sprint("localhost:", port) 113 s.zkAddr = fmt.Sprint("localhost:", port)
113 114
114 s.zkServer, err = zk.CreateServer(port, s.zkTestRoot, "") 115 s.zkServer, err = zk.CreateServer(port, s.zkTestRoot, "")
115 if err != nil { 116 if err != nil {
116 c.Fatal("Cannot set up server environment: ", err) 117 c.Fatal("Cannot set up server environment: ", err)
117 } 118 }
118 err = s.zkServer.Start() 119 err = s.zkServer.Start()
119 if err != nil { 120 if err != nil {
120 c.Fatal("Cannot start ZooKeeper server: ", err) 121 c.Fatal("Cannot start ZooKeeper server: ", err)
121 } 122 }
122 } 123 }
123 124
124 func (s *S) TearDownSuite(c *C) { 125 func (s *S) TearDownSuite(c *C) {
125 if s.zkServer != nil { 126 if s.zkServer != nil {
126 s.zkServer.Destroy() 127 s.zkServer.Destroy()
127 } 128 }
128 } 129 }
OLDNEW
« no previous file with comments | « retry_test.go ('k') | zk.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b