func ExampleInstanceSetSource_Latest_compare() { e := makeConnection() svipAddress := "my_vip" source, err := e.NewInstanceSetSourceForSecureVIPAddress(svipAddress, true, fargo.WithStatus(fargo.DOWN), fargo.WithStatus(fargo.OUTOFSERVICE)) if err != nil { fmt.Println(err) return } defer source.Stop() var troubled []*fargo.Instance for remaining := 10; ; { if instances := source.Latest(); instances != nil { fmt.Printf("VIP address %q has %d troubled instances.", svipAddress, len(instances)) switch diff := len(instances) - len(troubled); { case diff > 0: fmt.Printf(" (%d more than last time)\n", diff) case diff < 0: fmt.Printf(" (%d fewer than last time)\n", diff) default: fmt.Println() } troubled = instances } remaining-- if remaining == 0 { break } time.Sleep(30 * time.Second) } }
func TestGetSingleInstanceByVIPAddress(t *testing.T) { if testing.Short() { t.SkipNow() } e, _ := fargo.NewConnFromConfigFile("./config_sample/local.gcfg") cacheDelay := 35 * time.Second vipAddress := "app" for _, e.UseJson = range []bool{false, true} { Convey("When the VIP address has one instance", t, withRegisteredInstance(&e, func(instance *fargo.Instance) { time.Sleep(cacheDelay) instances, err := e.GetInstancesByVIPAddress(vipAddress) So(err, ShouldBeNil) So(instances, ShouldHaveLength, 1) So(instances[0].VipAddress, ShouldEqual, vipAddress) Convey("requesting the instances by that VIP address with status UP should provide that one", func() { instances, err := e.GetInstancesByVIPAddress(vipAddress, fargo.ThatAreUp) So(err, ShouldBeNil) So(instances, ShouldHaveLength, 1) So(instances[0].VipAddress, ShouldEqual, vipAddress) Convey("and when the instance has a status other than UP", func() { otherStatus := fargo.OUTOFSERVICE So(otherStatus, ShouldNotEqual, fargo.UP) err := e.UpdateInstanceStatus(instance, otherStatus) So(err, ShouldBeNil) Convey("selecting instances with that other status should provide that one", func() { time.Sleep(cacheDelay) instances, err := e.GetInstancesByVIPAddress(vipAddress, fargo.WithStatus(otherStatus)) So(err, ShouldBeNil) So(instances, ShouldHaveLength, 1) Convey("And selecting instances with status UP should provide none", func() { // Ensure that we tolerate a nil option safely. instances, err := e.GetInstancesByVIPAddress(vipAddress, fargo.ThatAreUp, nil) So(err, ShouldBeNil) So(instances, ShouldBeEmpty) }) }) }) }) })) Convey("When the secure VIP address has one instance", t, withRegisteredInstance(&e, func(_ *fargo.Instance) { Convey("requesting the instances by that VIP address should provide that one", func() { time.Sleep(cacheDelay) // Ensure that we tolerate a nil option safely. instances, err := e.GetInstancesBySecureVIPAddress(vipAddress, nil) So(err, ShouldBeNil) So(instances, ShouldHaveLength, 1) So(instances[0].SecureVipAddress, ShouldEqual, vipAddress) }) })) time.Sleep(cacheDelay) } }
func ExampleEurekaConnection_ScheduleVIPAddressUpdates_context() { e := makeConnection() ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) defer cancel() vipAddress := "my_vip" // Look for instances that are in trouble. updates, err := e.ScheduleVIPAddressUpdates(vipAddress, true, ctx.Done(), fargo.WithStatus(fargo.DOWN), fargo.WithStatus(fargo.OUTOFSERVICE)) if err != nil { fmt.Println(err) return } fmt.Printf("Monitoring VIP address %q.\n", vipAddress) for update := range updates { if update.Err != nil { fmt.Printf("Most recent request for VIP address %q's instances failed: %v\n", vipAddress, update.Err) continue } fmt.Printf("VIP address %q has %d instances in trouble.\n", vipAddress, len(update.Instances)) } fmt.Printf("Done monitoring VIP address %q.\n", vipAddress) }