github.com/weaviate/weaviate@v1.24.6/usecases/cluster/iterator_test.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package cluster
    13  
    14  import (
    15  	"testing"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  func TestNodeIteration(t *testing.T) {
    22  	source := []string{"node1", "node2", "node3", "node4"}
    23  	it, err := NewNodeIterator(source, StartRandom)
    24  	require.Nil(t, err)
    25  
    26  	found := map[string]int{}
    27  
    28  	for i := 0; i < 20; i++ {
    29  		host := it.Next()
    30  		found[host]++
    31  	}
    32  
    33  	// each host must be contained 5 times
    34  	assert.Equal(t, found["node1"], 5)
    35  	assert.Equal(t, found["node2"], 5)
    36  	assert.Equal(t, found["node3"], 5)
    37  	assert.Equal(t, found["node4"], 5)
    38  }
    39  
    40  func TestNodeIterationStartAfter(t *testing.T) {
    41  	source := []string{"node1", "node2", "node3", "node4"}
    42  	it, err := NewNodeIterator(source, StartAfter)
    43  	it.SetStartNode("node2")
    44  	require.Nil(t, err)
    45  
    46  	iterations := 3
    47  	found := make([]string, iterations)
    48  	for i := 0; i < iterations; i++ {
    49  		host := it.Next()
    50  		found[i] = host
    51  	}
    52  
    53  	expected := []string{"node3", "node4", "node1"}
    54  	assert.Equal(t, expected, found)
    55  }