github.com/erda-project/erda-infra@v1.0.9/providers/etcd-election/election_test.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package election
    16  
    17  import (
    18  	"context"
    19  	"sync/atomic"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/coreos/etcd/clientv3"
    24  	"github.com/coreos/etcd/integration"
    25  
    26  	"github.com/erda-project/erda-infra/base/logs/logrusx"
    27  )
    28  
    29  var waitTime = 300 * time.Millisecond
    30  
    31  func TestElection(t *testing.T) {
    32  	cfg := integration.ClusterConfig{Size: 1}
    33  	clus := integration.NewClusterV3(t, &cfg)
    34  	defer clus.Terminate(t)
    35  	endpoints := []string{clus.Client(0).Endpoints()[0]}
    36  	cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
    37  	if err != nil {
    38  		t.FailNow()
    39  	}
    40  
    41  	primary := &provider{Cfg: &config{Prefix: "/election"}, Log: logrusx.New(), Client: cli}
    42  	secondary := &provider{Cfg: &config{Prefix: "/election"}, Log: logrusx.New(), Client: cli}
    43  
    44  	leaderSet := int32(0)
    45  	primary.Init(nil)
    46  	primary.OnLeader(func(ctx context.Context) {
    47  		atomic.AddInt32(&leaderSet, 1)
    48  	})
    49  
    50  	ctx, cancel := context.WithCancel(context.Background())
    51  	go primary.Run(ctx)
    52  	time.Sleep(waitTime)
    53  	if !primary.IsLeader() || atomic.LoadInt32(&leaderSet) == 0 {
    54  		t.Fatalf("no leader is selected")
    55  	}
    56  
    57  	leader, err := primary.Leader()
    58  	if leader == nil || err != nil {
    59  		t.Fatalf("leader function failed")
    60  	}
    61  
    62  	secondary.Init(nil)
    63  	secondaryCtx, secondaryCancel := context.WithCancel(context.Background())
    64  	defer secondaryCancel()
    65  	go secondary.Run(secondaryCtx)
    66  	time.Sleep(waitTime)
    67  	if secondary.IsLeader() {
    68  		t.Fatalf("secondary should not be elected")
    69  	}
    70  
    71  	leader, err = secondary.Leader()
    72  	if leader == nil || err != nil {
    73  		t.Fatalf("leader function failed")
    74  	}
    75  
    76  	// primary exit
    77  	cancel()
    78  
    79  	time.Sleep(waitTime)
    80  	if !secondary.IsLeader() {
    81  		t.Fatalf("secondary should be elected")
    82  	}
    83  }
    84  
    85  func TestElectionOnClusterReboot(t *testing.T) {
    86  	cfg := integration.ClusterConfig{Size: 1}
    87  	clus := integration.NewClusterV3(t, &cfg)
    88  	defer clus.Terminate(t)
    89  	endpoints := []string{clus.Client(0).Endpoints()[0]}
    90  	cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
    91  	if err != nil {
    92  		t.FailNow()
    93  	}
    94  
    95  	primary := &provider{Cfg: &config{Prefix: "/election", NodeID: "primary"}, Log: logrusx.New(), Client: cli}
    96  	secondary := &provider{Cfg: &config{Prefix: "/election", NodeID: "secondary"}, Log: logrusx.New(), Client: cli}
    97  
    98  	primary.Init(nil)
    99  	ctx, cancel := context.WithCancel(context.Background())
   100  	defer cancel()
   101  	go primary.Run(ctx)
   102  
   103  	time.Sleep(150 * time.Millisecond)
   104  	if !primary.IsLeader() {
   105  		t.Fatalf("no leader is selected")
   106  	}
   107  
   108  	secondary.Init(nil)
   109  	secondaryCtx, secondaryCancel := context.WithCancel(context.Background())
   110  	defer secondaryCancel()
   111  	go secondary.Run(secondaryCtx)
   112  	time.Sleep(waitTime)
   113  
   114  	// simulate secondary losing connection
   115  	secondary.lock.Lock()
   116  	secondary.session.Close()
   117  	secondary.lock.Unlock()
   118  	// primary quit
   119  	cancel()
   120  
   121  	time.Sleep(waitTime)
   122  	if !secondary.IsLeader() {
   123  		t.Fatalf("secondary should be leader now")
   124  	}
   125  }