func EnsurePath(z *zk.Conn, pathParts []string, perm []zk.ACL) (err error) { path := BuildPath(pathParts) stat, err := z.Exists(path) if err != nil || stat != nil { return } currentParts := make([]string, 0, len(pathParts)) for _, part := range pathParts { currentParts = append(currentParts, part) _, err = z.Create(BuildPath(currentParts), "", 0, perm) if err != nil { if zkErr, ok := err.(*zk.Error); ok && zkErr.Code == zk.ZNODEEXISTS { err = nil } else { return } } } return }
Expect(err).NotTo(HaveOccurred()) Expect((<-session).State).To(Equal(zk.STATE_CONNECTED)) z = zz }) AfterEach(func() { z.Close() s.Stop() }) It("should ensure path", func() { perm := zk.WorldACL(zk.PERM_ALL) stat, err := z.Exists("/part1") Expect(err).NotTo(HaveOccurred()) Expect(stat).To(BeNil()) err = EnsurePath(z, []string{"part1"}, perm) Expect(err).NotTo(HaveOccurred()) stat, err = z.Exists("/part1") Expect(err).NotTo(HaveOccurred()) Expect(stat).NotTo(BeNil()) err = EnsurePath(z, []string{"part1"}, perm) Expect(err).NotTo(HaveOccurred()) err = EnsurePath(z, []string{"part1", "part2", "part3"}, perm) Expect(err).NotTo(HaveOccurred())
r = rr }) AfterEach(func() { z.Close() if r != nil { r.Close() } s.Stop() }) It("should register service", func() { stat, err := z.Exists("/services/myservice/proto") Expect(err).NotTo(HaveOccurred()) Expect(stat).To(BeNil()) err = r.Register("myservice", "proto", "localhost:1234") Expect(err).NotTo(HaveOccurred()) stat, err = z.Exists("/services/myservice/proto") Expect(err).NotTo(HaveOccurred()) Expect(stat).NotTo(BeNil()) children, _, err := z.Children("/services/myservice/proto") Expect(err).NotTo(HaveOccurred()) Expect(children).To(HaveLen(1)) data, _, err := z.Get("/services/myservice/proto/" + children[0])