github.com/dubbogo/gost@v1.14.0/database/kv/zk/client_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package gxzookeeper
    19  
    20  import (
    21  	"strconv"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  import (
    27  	"github.com/dubbogo/go-zookeeper/zk"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func verifyEventStateOrder(t *testing.T, c <-chan zk.Event, expectedStates []zk.State, source string) {
    33  	for _, state := range expectedStates {
    34  		for {
    35  			event, ok := <-c
    36  			if !ok {
    37  				t.Fatalf("unexpected channel close for %s", source)
    38  			}
    39  			if event.Type != zk.EventSession {
    40  				continue
    41  			}
    42  
    43  			if event.State != state {
    44  				t.Fatalf("mismatched state order from %s, expected %v, received %v", source, state, event.State)
    45  			}
    46  			break
    47  		}
    48  	}
    49  }
    50  
    51  func Test_getZookeeperClient(t *testing.T) {
    52  	var err error
    53  	var tc *zk.TestCluster
    54  	var address []string
    55  	tc, err = zk.StartTestCluster(1, nil, nil, zk.WithRetryTimes(40))
    56  	assert.NoError(t, err)
    57  	assert.NotNil(t, tc.Servers[0])
    58  
    59  	address = append(address, "127.0.0.1:"+strconv.Itoa(tc.Servers[0].Port))
    60  	client1, err := NewZookeeperClient("test1", address, true, WithZkTimeOut(3*time.Second))
    61  	assert.Nil(t, err)
    62  	client2, err := NewZookeeperClient("test1", address, true, WithZkTimeOut(3*time.Second))
    63  	assert.Nil(t, err)
    64  	client3, err := NewZookeeperClient("test2", address, true, WithZkTimeOut(3*time.Second))
    65  	assert.Nil(t, err)
    66  	client4, err := NewZookeeperClient("test2", address, false, WithZkTimeOut(3*time.Second))
    67  	assert.Nil(t, err)
    68  	if client1 != client2 {
    69  		t.Fatalf("NewZookeeperClient failed")
    70  	}
    71  	if client1 == client3 {
    72  		t.Fatalf("NewZookeeperClient failed")
    73  	}
    74  	if client3 == client4 {
    75  		t.Fatalf("NewZookeeperClient failed")
    76  	}
    77  	client1.Close()
    78  	client2.Close()
    79  	client3.Close()
    80  	client4.Close()
    81  	tc.Stop()
    82  }
    83  
    84  func Test_Close(t *testing.T) {
    85  	var err error
    86  	var tc *zk.TestCluster
    87  	var address []string
    88  	tc, err = zk.StartTestCluster(1, nil, nil, zk.WithRetryTimes(40))
    89  	assert.NoError(t, err)
    90  	assert.NotNil(t, tc.Servers[0])
    91  	address = append(address, "127.0.0.1:"+strconv.Itoa(tc.Servers[0].Port))
    92  	client1, err := NewZookeeperClient("test1", address, true, WithZkTimeOut(3*time.Second))
    93  	assert.Nil(t, err)
    94  	client2, err := NewZookeeperClient("test1", address, true, WithZkTimeOut(3*time.Second))
    95  	assert.Nil(t, err)
    96  	if client1 != client2 {
    97  		t.Fatalf("NewZookeeperClient failed")
    98  	}
    99  	client1.Close()
   100  	client3, err := NewZookeeperClient("test1", address, true, WithZkTimeOut(3*time.Second))
   101  	assert.Nil(t, err)
   102  	if client2 != client3 {
   103  		t.Fatalf("NewZookeeperClient failed")
   104  	}
   105  	client2.Close()
   106  	assert.Equal(t, client1.activeNumber, uint32(1))
   107  	client1.Close()
   108  	assert.Equal(t, client1.activeNumber, uint32(0))
   109  	client4, err := NewZookeeperClient("test1", address, true, WithZkTimeOut(3*time.Second))
   110  	assert.Nil(t, err)
   111  	assert.Equal(t, client4.activeNumber, uint32(1))
   112  	if client4 == client3 {
   113  		t.Fatalf("NewZookeeperClient failed")
   114  	}
   115  	client5, err := NewZookeeperClient("test1", address, false, WithZkTimeOut(3*time.Second))
   116  	assert.Nil(t, err)
   117  	client6, err := NewZookeeperClient("test1", address, false, WithZkTimeOut(3*time.Second))
   118  	assert.Nil(t, err)
   119  	if client5 == client6 {
   120  		t.Fatalf("NewZookeeperClient failed")
   121  	}
   122  	client5.Close()
   123  	assert.Equal(t, client5.activeNumber, uint32(0))
   124  	assert.Equal(t, client5.Conn, (*zk.Conn)(nil))
   125  	assert.NotEqual(t, client6.Conn, nil)
   126  	client6.Close()
   127  	assert.Equal(t, client6.activeNumber, uint32(0))
   128  	assert.Equal(t, client6.Conn, (*zk.Conn)(nil))
   129  	tc.Stop()
   130  }
   131  
   132  func Test_newMockZookeeperClient(t *testing.T) {
   133  	ts, _, event, err := NewMockZookeeperClient("test", 15*time.Second)
   134  	assert.NoError(t, err)
   135  	defer func() {
   136  		err := ts.Stop()
   137  		assert.Nil(t, err)
   138  	}()
   139  	states := []zk.State{zk.StateConnecting, zk.StateConnected, zk.StateHasSession}
   140  	verifyEventStateOrder(t, event, states, "event channel")
   141  }
   142  
   143  func TestCreate(t *testing.T) {
   144  	ts, z, event, err := NewMockZookeeperClient("test", 15*time.Second)
   145  	assert.NoError(t, err)
   146  	defer func() {
   147  		_ = ts.Stop()
   148  		assert.Nil(t, err)
   149  	}()
   150  	err = z.Create("/test1/test2/test3/test4")
   151  	assert.NoError(t, err)
   152  
   153  	states := []zk.State{zk.StateConnecting, zk.StateConnected, zk.StateHasSession}
   154  	verifyEventStateOrder(t, event, states, "event channel")
   155  }
   156  
   157  func TestCreateDelete(t *testing.T) {
   158  	ts, z, event, err := NewMockZookeeperClient("test", 15*time.Second)
   159  	assert.NoError(t, err)
   160  	defer func() {
   161  		_ = ts.Stop()
   162  		assert.Nil(t, err)
   163  	}()
   164  
   165  	states := []zk.State{zk.StateConnecting, zk.StateConnected, zk.StateHasSession}
   166  	verifyEventStateOrder(t, event, states, "event channel")
   167  	err = z.Create("/test1/test2/test3/test4")
   168  	assert.NoError(t, err)
   169  	err = z.Delete("/test1/test2/test3/test4")
   170  	assert.NoError(t, err)
   171  	// verifyEventOrder(t, event, []zk.EventType{zk.EventNodeCreated}, "event channel")
   172  }
   173  
   174  func TestRegisterTemp(t *testing.T) {
   175  	ts, z, event, err := NewMockZookeeperClient("test", 15*time.Second)
   176  	assert.NoError(t, err)
   177  	defer func() {
   178  		_ = ts.Stop()
   179  		assert.Nil(t, err)
   180  	}()
   181  	err = z.Create("/test1/test2/test3")
   182  	assert.NoError(t, err)
   183  
   184  	tmpath, err := z.RegisterTemp("/test1/test2/test3", "test4")
   185  	assert.NoError(t, err)
   186  	assert.Equal(t, "/test1/test2/test3/test4", tmpath)
   187  	states := []zk.State{zk.StateConnecting, zk.StateConnected, zk.StateHasSession}
   188  	verifyEventStateOrder(t, event, states, "event channel")
   189  }
   190  
   191  func TestRegisterTempSeq(t *testing.T) {
   192  	ts, z, event, err := NewMockZookeeperClient("test", 15*time.Second)
   193  	assert.NoError(t, err)
   194  	defer func() {
   195  		_ = ts.Stop()
   196  		assert.Nil(t, err)
   197  	}()
   198  	err = z.Create("/test1/test2/test3")
   199  	assert.NoError(t, err)
   200  	tmpath, err := z.RegisterTempSeq("/test1/test2/test3", []byte("test"))
   201  	assert.NoError(t, err)
   202  	assert.Equal(t, "/test1/test2/test3/0000000000", tmpath)
   203  	states := []zk.State{zk.StateConnecting, zk.StateConnected, zk.StateHasSession}
   204  	verifyEventStateOrder(t, event, states, "event channel")
   205  }
   206  
   207  func Test_UnregisterEvent(t *testing.T) {
   208  	client := &ZookeeperClient{}
   209  	client.eventRegistry = make(map[string][]chan zk.Event)
   210  	mockEvent := make(chan zk.Event, 1)
   211  	var array []chan zk.Event
   212  	array = append(array, mockEvent)
   213  	client.eventRegistry["test"] = array
   214  	client.UnregisterEvent("test", mockEvent)
   215  }