| 119 | } |
| 120 | |
| 121 | func (t *TestCluster) AddMember(e *environment.Environment) service.Service { |
| 122 | t.mu.Lock() |
| 123 | defer t.mu.Unlock() |
| 124 | |
| 125 | if e == nil { |
| 126 | e = NewEnvironment(nil) |
| 127 | } |
| 128 | c := e.Get("config").(*config.Config) |
| 129 | partitions.SetHashFunc(c.Hasher) |
| 130 | |
| 131 | port, err := testutil.GetFreePort() |
| 132 | if err != nil { |
| 133 | panic(fmt.Sprintf("failed to a random port: %v", err)) |
| 134 | } |
| 135 | c.MemberlistConfig.BindPort = port |
| 136 | |
| 137 | var peers []string |
| 138 | for _, peerPort := range t.memberPorts { |
| 139 | peers = append(peers, net.JoinHostPort("127.0.0.1", strconv.Itoa(peerPort))) |
| 140 | } |
| 141 | c.Peers = peers |
| 142 | |
| 143 | s := t.newService(e) |
| 144 | rt := e.Get("routingtable").(*routingtable.RoutingTable) |
| 145 | err = rt.Join() |
| 146 | if err != nil { |
| 147 | panic(fmt.Sprintf("failed to join the Olric cluster: %v", err)) |
| 148 | } |
| 149 | err = rt.Start() |
| 150 | if err != nil { |
| 151 | panic(fmt.Sprintf("failed to start the routing table: %v", err)) |
| 152 | } |
| 153 | |
| 154 | t.errGr.Go(func() error { |
| 155 | <-t.ctx.Done() |
| 156 | return rt.Shutdown(context.Background()) |
| 157 | }) |
| 158 | |
| 159 | t.errGr.Go(func() error { |
| 160 | return s.Start() |
| 161 | }) |
| 162 | |
| 163 | t.errGr.Go(func() error { |
| 164 | <-t.ctx.Done() |
| 165 | return s.Shutdown(context.Background()) |
| 166 | }) |
| 167 | |
| 168 | t.environments = append(t.environments, e) |
| 169 | t.memberPorts = append(t.memberPorts, port) |
| 170 | t.syncCluster() |
| 171 | return s |
| 172 | } |
| 173 | |
| 174 | func (t *TestCluster) Shutdown() { |
| 175 | t.cancel() |