func TestKeysSetExpire(t *testing.T) { key := NewKey("new") key = key.SetKey("1") NewRedisMock() Cache.Mock.Command("SET", "new:1", []byte("hello")) Cache.Mock.Command("EXPIRE", "new:1", redigomock.NewAnyData()) err := key.Set([]byte("hello")) assert.NoError(t, err, "An error was not expected") }
func TestSetMirrorEnabled(t *testing.T) { mock, conn := PrepareRedisTest() cmd_publish := mock.Command("PUBLISH", string(database.MIRROR_UPDATE), redigomock.NewAnyData()).Expect("ok") cmd_enable := mock.Command("HMSET", "MIRROR_m1", "enabled", true).Expect("ok") SetMirrorEnabled(conn, "m1", true) if mock.Stats(cmd_enable) < 1 { t.Fatalf("Mirror not enabled") } else if mock.Stats(cmd_enable) > 1 { t.Fatalf("Mirror enabled more than once") } if mock.Stats(cmd_publish) < 1 { t.Fatalf("Event MIRROR_UPDATE not published") } mock.Command("HMSET", "MIRROR_m1", "enabled", true).ExpectError(redis.Error("blah")) if SetMirrorEnabled(conn, "m1", true) == nil { t.Fatalf("Error expected") } cmd_disable := mock.Command("HMSET", "MIRROR_m1", "enabled", false).Expect("ok") SetMirrorEnabled(conn, "m1", false) if mock.Stats(cmd_disable) != 1 { t.Fatalf("Mirror not disabled") } else if mock.Stats(cmd_disable) > 1 { t.Fatalf("Mirror disabled more than once") } if mock.Stats(cmd_publish) < 2 { t.Fatalf("Event MIRROR_UPDATE not published") } mock.Command("HMSET", "MIRROR_m1", "enabled", false).ExpectError(redis.Error("blah")) if SetMirrorEnabled(conn, "m1", false) == nil { t.Fatalf("Error expected") } }
func TestSetMirrorState(t *testing.T) { mock, conn := PrepareRedisTest() if err := SetMirrorState(conn, "m1", true, "test1"); err == nil { t.Fatalf("Error expected but nil returned") } cmd_publish := mock.Command("PUBLISH", string(database.MIRROR_UPDATE), redigomock.NewAnyData()).Expect("ok") /* */ cmd_previous_state := mock.Command("HGET", "MIRROR_m1", "up").Expect(int64(0)).Expect(int64(1)) cmd_state_since := mock.Command("HMSET", "MIRROR_m1", "up", true, "excludeReason", "test1", "stateSince", redigomock.NewAnyInt()).Expect("ok") cmd_state := mock.Command("HMSET", "MIRROR_m1", "up", true, "excludeReason", "test2").Expect("ok") if err := SetMirrorState(conn, "m1", true, "test1"); err != nil { t.Fatalf("Unexpected error: %s", err) } if mock.Stats(cmd_previous_state) < 1 { t.Fatalf("Previous state not tested") } if mock.Stats(cmd_state_since) < 1 { t.Fatalf("New state not set") } else if mock.Stats(cmd_state_since) > 1 { t.Fatalf("State set more than once") } if mock.Stats(cmd_publish) < 1 { t.Fatalf("Event MIRROR_UPDATE not published") } /* */ if err := SetMirrorState(conn, "m1", true, "test2"); err != nil { t.Fatalf("Unexpected error: %s", err) } if mock.Stats(cmd_state_since) > 1 || mock.Stats(cmd_state) < 1 { t.Fatalf("The value stateSince isn't supposed to be set") } if mock.Stats(cmd_publish) != 1 { t.Fatalf("Event MIRROR_UPDATE should not be sent") } /* */ cmd_previous_state = mock.Command("HGET", "MIRROR_m1", "up").Expect(int64(1)) cmd_state_since = mock.Command("HMSET", "MIRROR_m1", "up", false, "excludeReason", "test3", "stateSince", redigomock.NewAnyInt()).Expect("ok") if err := SetMirrorState(conn, "m1", false, "test3"); err != nil { t.Fatalf("Unexpected error: %s", err) } if mock.Stats(cmd_previous_state) < 1 { t.Fatalf("Previous state not tested") } if mock.Stats(cmd_state_since) < 1 { t.Fatalf("New state not set") } else if mock.Stats(cmd_state_since) > 1 { t.Fatalf("State set more than once") } if mock.Stats(cmd_publish) < 2 { t.Fatalf("Event MIRROR_UPDATE not published") } }