MCPcopy Create free account
hub / github.com/DoNewsCode/core / Example_providers

Function Example_providers

leader/example_providers_test.go:25–48  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

23}
24
25func Example_providers() {
26 if os.Getenv("ETCD_ADDR") == "" {
27 fmt.Println("set ETCD_ADDR to run this example")
28 return
29 }
30 c := core.Default(core.WithInline("log.level", "none"))
31 c.Provide(leader.Providers(leader.WithDriver(AlwaysLeaderDriver{})))
32
33 c.Invoke(func(dispatcher contract.Dispatcher, sts *leader.Status) {
34 dispatcher.Subscribe(events.Listen(leader.OnStatusChanged, func(ctx context.Context, event interface{}) error {
35 // Becomes true when campaign succeeds and becomes false when resign
36 fmt.Println(event.(leader.OnStatusChangedPayload).Status.IsLeader())
37 return nil
38 }))
39 })
40
41 ctx, cancel := context.WithTimeout(context.Background(), time.Second)
42 defer cancel()
43 c.Serve(ctx)
44
45 // Output:
46 // true
47 // false
48}

Callers

nothing calls this directly

Calls 10

DefaultFunction · 0.92
WithInlineFunction · 0.92
ProvidersFunction · 0.92
WithDriverFunction · 0.92
ListenFunction · 0.92
ProvideMethod · 0.80
InvokeMethod · 0.80
IsLeaderMethod · 0.80
ServeMethod · 0.80
SubscribeMethod · 0.65

Tested by

no test coverage detected