Exemplo n.º 1
0
	BeforeEach(func() {
		client = redis.NewClient(&redis.Options{
			Addr: redisAddr,
		})
	})

	AfterEach(func() {
		Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
		Expect(client.Close()).NotTo(HaveOccurred())
	})

	It("should implement Stringer", func() {
		set := client.Set("foo", "bar", 0)
		Expect(set.String()).To(Equal("SET foo bar: OK"))

		get := client.Get("foo")
		Expect(get.String()).To(Equal("GET foo: bar"))
	})

	It("should have correct val/err states", func() {
		set := client.Set("key", "hello", 0)
		Expect(set.Err()).NotTo(HaveOccurred())
		Expect(set.Val()).To(Equal("OK"))

		get := client.Get("key")
		Expect(get.Err()).NotTo(HaveOccurred())
		Expect(get.Val()).To(Equal("hello"))

		Expect(set.Err()).NotTo(HaveOccurred())
		Expect(set.Val()).To(Equal("OK"))
	})
Exemplo n.º 2
0
		key := "TestPipelineIncr"
		pipeline := client.Pipeline()
		for i := 0; i < N; i++ {
			pipeline.Incr(key)
		}

		cmds, err := pipeline.Exec()
		Expect(err).NotTo(HaveOccurred())
		Expect(pipeline.Close()).NotTo(HaveOccurred())

		Expect(len(cmds)).To(Equal(20000))
		for _, cmd := range cmds {
			Expect(cmd.Err()).NotTo(HaveOccurred())
		}

		get := client.Get(key)
		Expect(get.Err()).NotTo(HaveOccurred())
		Expect(get.Val()).To(Equal(strconv.Itoa(N)))
	})

	It("should PipelineEcho", func() {
		const N = 1000

		wg := &sync.WaitGroup{}
		wg.Add(N)
		for i := 0; i < N; i++ {
			go func(i int) {
				defer GinkgoRecover()
				defer wg.Done()

				pipeline := client.Pipeline()
Exemplo n.º 3
0
			return err
		}

		var wg sync.WaitGroup
		for i := 0; i < 100; i++ {
			wg.Add(1)
			go func() {
				defer wg.Done()

				err := incr("key")
				Expect(err).NotTo(HaveOccurred())
			}()
		}
		wg.Wait()

		n, err := client.Get("key").Int64()
		Expect(err).NotTo(HaveOccurred())
		Expect(n).To(Equal(int64(100)))
	})

	It("should discard", func() {
		multi := client.Multi()
		defer func() {
			Expect(multi.Close()).NotTo(HaveOccurred())
		}()

		cmds, err := multi.Exec(func() error {
			multi.Set("key1", "hello1", 0)
			multi.Discard()
			multi.Set("key2", "hello2", 0)
			return nil
Exemplo n.º 4
0
		}, "10s", "100ms").Should(ContainSubstring("slaves=2"))

		// Kill master.
		sentinelMaster.Shutdown()
		Eventually(func() error {
			return sentinelMaster.Ping().Err()
		}, "5s", "100ms").Should(HaveOccurred())

		// Wait for Redis sentinel to elect new master.
		Eventually(func() string {
			return sentinelSlave1.Info().Val() + sentinelSlave2.Info().Val()
		}, "30s", "1s").Should(ContainSubstring("role:master"))

		// Check that client picked up new master.
		Eventually(func() error {
			return client.Get("foo").Err()
		}, "5s", "100ms").ShouldNot(HaveOccurred())
	})

	It("supports DB selection", func() {
		Expect(client.Close()).NotTo(HaveOccurred())

		client = redis.NewFailoverClient(&redis.FailoverOptions{
			MasterName:    sentinelName,
			SentinelAddrs: []string{":" + sentinelPort},
			DB:            1,
		})
		err := client.Ping().Err()
		Expect(err).NotTo(HaveOccurred())
	})
})
Exemplo n.º 5
0
		Expect(idle.Ping().Err()).NotTo(HaveOccurred())
		time.Sleep(time.Millisecond)
		Expect(idle.Ping().Err()).NotTo(HaveOccurred())
	})

	It("should support DB selection", func() {
		db1 := redis.NewClient(&redis.Options{
			Addr: redisAddr,
			DB:   1,
		})
		defer db1.Close()

		Expect(db1.Get("key").Err()).To(Equal(redis.Nil))
		Expect(db1.Set("key", "value", 0).Err()).NotTo(HaveOccurred())

		Expect(client.Get("key").Err()).To(Equal(redis.Nil))
		Expect(db1.Get("key").Val()).To(Equal("value"))
		Expect(db1.FlushDb().Err()).NotTo(HaveOccurred())
	})

	It("should support DB selection with read timeout (issue #135)", func() {
		for i := 0; i < 100; i++ {
			db1 := redis.NewClient(&redis.Options{
				Addr:        redisAddr,
				DB:          1,
				ReadTimeout: time.Nanosecond,
			})

			err := db1.Ping().Err()
			Expect(err).To(HaveOccurred())
			Expect(err.(net.Error).Timeout()).To(BeTrue())