// New creates a new Group object. func New(opts *GroupOpts) (*Group, error) { if err := opts.Valid(); err != nil { return nil, err } g := &Group{} // Add logger to group. if opts.Log != nil { g.log = opts.Log.New("machines") } else { g.log = machine.DefaultLogger.New("machines") } // Use default discover client when not set. if opts.Discover != nil { g.discover = opts.Discover } else { g.discover = discover.NewClient() } // Create dynamic clients. var err error g.client, err = clients.New(&clients.ClientsOpts{ Builder: opts.Builder, DynAddrInterval: opts.DynAddrInterval, PingInterval: opts.PingInterval, Log: g.log, }) if err != nil { g.log.Critical("Cannot create machine monitor: %s", err) return nil, err } // Add default components. g.address = addresses.New() g.alias = aliases.New() if opts.Storage == nil { return g, nil } // Try to add storage for Addresses. if address, err := addresses.NewCached(opts.Storage); err != nil { g.log.Warning("Cannot load addresses cache: %s", err) } else { g.address = address } // Try to add storage for Aliases. if alias, err := aliases.NewCached(opts.Storage); err != nil { g.log.Warning("Cannot load aliases cache: %s", err) } else { g.alias = alias } // Start memory workers. g.bootstrap() return g, nil }
func TestClients(t *testing.T) { var ( builder = testutil.NewBuilder(nil) idA = machine.ID("servA") idB = machine.ID("servB") servA = &testutil.Server{} servB = &testutil.Server{} ) cs, err := clients.New(testOptions(builder)) if err != nil { t.Fatalf("want err = nil; got %v", err) } defer cs.Close() var g errgroup.Group create := map[machine.ID]client.DynamicAddrFunc{ idA: servA.AddrFunc(), idB: servB.AddrFunc(), idA: servA.AddrFunc(), // duplicate. idA: servA.AddrFunc(), // duplicate. } for id, dynAddr := range create { id, dynAddr := id, dynAddr // Local copy for concurrency. g.Go(func() error { return cs.Create(id, dynAddr) }) } if err := g.Wait(); err != nil { t.Fatalf("want err = nil; got %v", err) } if regs := cs.Registered(); len(regs) != 2 { t.Fatalf("want clients count = 2; got %d", len(regs)) } if _, err := cs.Client(idA); err != nil { t.Fatalf("want err = nil; got %v", err) } if err := cs.Drop(idA); err != nil { t.Fatalf("want err = nil; got %v", err) } if _, err := cs.Client(idA); err != machine.ErrMachineNotFound { t.Fatalf("want machine not found; got %v", err) } if regs := cs.Registered(); len(regs) != 1 { t.Fatalf("want clients count = 1; got %d", len(regs)) } }