Exemplo n.º 1
0
		fakeConnection *fake.FakeConnection
	)

	BeforeEach(func() {
		fakeConnection = &fake.FakeConnection{}
		commander = &mcredis.RedisInstance{fake.NewFakeCommander()}
	})

	Describe("#Set", func() {
		It("calls SET on the redis connection", func() {
			var setCalled bool
			fakeConnection.RespondToDo(func(command string, args ...interface{}) (interface{}, error) {
				setCalled = true
				Expect(command).To(Equal("SET"))
				Expect(args[0]).To(HaveLen(2))
				Expect(args[0].([]interface{})[0]).To(Equal("my key"))
				Expect(args[0].([]interface{})[1]).To(Equal("my value"))

				return nil, nil
			})

			err := commander.Set("my key", "my value")
			Expect(err).To(BeNil())
			Expect(setCalled).To(BeTrue())
		})

		It("returns the redis error", func() {
			fakeConnection.RespondToDo(func(command string, args ...interface{}) (interface{}, error) {
				return nil, errors.New("set error")
			})
Exemplo n.º 2
0
			}
			return redis, nil
		})

		redisDialer.RespondToDialTimeout(func(url, address string, connectTimeout, readTimeout, writeTimeout time.Duration) (*fake.FakeConnection, error) {
			dialSentinelCount++
			if address == "badSentinel" {
				return nil, errors.New("sentinel connection error")
			}
			return redis, nil
		})

		redis.RespondToDo(func(commandName string, args ...interface{}) (reply interface{}, err error) {
			if commandName == "SENTINEL" {
				return []interface{}{"masterAddr", "port"}, nil
			} else {
				roleCalled = true
				return "role:master", nil
			}
		})
	})

	Describe("#ConnectAndGetMaster", func() {
		It("connects to a sentinel and returns the master connection", func() {
			sentinel = mcredis.NewSentinel(redisDialer, "master", "goodSentinel", 0)
			masterConn, err := sentinel.ConnectAndGetMaster()

			Expect(err).To(BeNil())
			Expect(dialSentinelCount).To(Equal(1))
			Expect(dialMasterCount).To(Equal(1))
			Expect(roleCalled).To(BeTrue())
			Expect(masterConn).To(Equal(redis))