github.com/m3db/m3@v1.5.0/src/aggregator/integration/election.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package integration
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  	clientv3 "go.etcd.io/etcd/client/v3"
    28  	"go.etcd.io/etcd/tests/v3/framework/integration"
    29  
    30  	"github.com/m3db/m3/src/cluster/services"
    31  	"github.com/m3db/m3/src/cluster/services/leader"
    32  )
    33  
    34  var (
    35  	testClusterSize     = 1
    36  	testEnvironment     = "testEnv"
    37  	testServiceName     = "testSvc"
    38  	testZone            = "testZone"
    39  	testElectionTTLSecs = 5
    40  )
    41  
    42  type testCluster struct {
    43  	t       *testing.T
    44  	cluster *integration.Cluster
    45  }
    46  
    47  func newTestCluster(t *testing.T) *testCluster {
    48  	integration.BeforeTestExternal(t)
    49  	return &testCluster{
    50  		t: t,
    51  		cluster: integration.NewCluster(t, &integration.ClusterConfig{
    52  			Size: testClusterSize,
    53  		}),
    54  	}
    55  }
    56  
    57  func (tc *testCluster) LeaderService() services.LeaderService {
    58  	svc, err := leader.NewService(tc.etcdClient(), tc.options())
    59  	require.NoError(tc.t, err)
    60  	return svc
    61  }
    62  
    63  func (tc *testCluster) Close() {
    64  	tc.cluster.Terminate(tc.t)
    65  }
    66  
    67  func (tc *testCluster) etcdClient() *clientv3.Client {
    68  	return tc.cluster.RandClient()
    69  }
    70  
    71  func (tc *testCluster) options() leader.Options {
    72  	sid := services.NewServiceID().
    73  		SetEnvironment(testEnvironment).
    74  		SetName(testServiceName).
    75  		SetZone(testZone)
    76  	eopts := services.NewElectionOptions().
    77  		SetTTLSecs(testElectionTTLSecs)
    78  	return leader.NewOptions().
    79  		SetServiceID(sid).
    80  		SetElectionOpts(eopts)
    81  }