vitess.io/vitess@v0.16.2/go/vt/vtgr/vtgr_test.go (about)

     1  package vtgr
     2  
     3  import (
     4  	"context"
     5  	"syscall"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"vitess.io/vitess/go/sync2"
    12  	"vitess.io/vitess/go/vt/topo/memorytopo"
    13  	"vitess.io/vitess/go/vt/vtgr/config"
    14  	"vitess.io/vitess/go/vt/vtgr/controller"
    15  	"vitess.io/vitess/go/vt/vtgr/db"
    16  	"vitess.io/vitess/go/vt/vttablet/tmclient"
    17  
    18  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    19  )
    20  
    21  func TestSighupHandle(t *testing.T) {
    22  	ctx := context.Background()
    23  	ts := memorytopo.NewServer("cell1")
    24  	defer ts.Close()
    25  	ts.CreateKeyspace(ctx, "ks", &topodatapb.Keyspace{})
    26  	ts.CreateShard(ctx, "ks", "0")
    27  	vtgr := newVTGR(
    28  		ctx,
    29  		ts,
    30  		tmclient.NewTabletManagerClient(),
    31  	)
    32  	var shards []*controller.GRShard
    33  	config := &config.VTGRConfig{
    34  		DisableReadOnlyProtection:   false,
    35  		BootstrapGroupSize:          5,
    36  		MinNumReplica:               3,
    37  		BackoffErrorWaitTimeSeconds: 10,
    38  		BootstrapWaitTimeSeconds:    10 * 60,
    39  	}
    40  	shards = append(shards, controller.NewGRShard("ks", "0", nil, vtgr.tmc, vtgr.topo, db.NewVTGRSqlAgent(), config, localDbPort, true))
    41  	vtgr.Shards = shards
    42  	shard := vtgr.Shards[0]
    43  	shard.LockShard(ctx, "test")
    44  	res := sync2.NewAtomicInt32(0)
    45  	vtgr.handleSignal(func(i int) {
    46  		res.Set(1)
    47  	})
    48  	assert.NotNil(t, shard.GetUnlock())
    49  	assert.False(t, vtgr.stopped.Get())
    50  	syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
    51  	time.Sleep(100 * time.Millisecond)
    52  	assert.Equal(t, int32(1), res.Get())
    53  	assert.Nil(t, shard.GetUnlock())
    54  	assert.True(t, vtgr.stopped.Get())
    55  }