File size: 3,253 Bytes
b110593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
//  CONTACT: [email protected]
//

package replica

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestResolver(t *testing.T) {
	ss := map[string][]string{
		"S0": {},
		"S1": {"A", "B", "C"},
		"S2": {"D", "E"},
		"S3": {"A", "B", "C", "D", "E"},
		"S4": {"D", "E", "F"},
		"S5": {"A", "B", "C", "D", "E", "F"},
	}
	assertSameHosts := func(want, got rState, thisNode string) {
		t.Helper()
		if thisNode != "" {
			assert.Equal(t, thisNode, got.Hosts[0])
		}
		assert.ElementsMatch(t, want.Hosts, got.Hosts, "match Hosts")
		assert.Equal(t, want.NodeMap, got.NodeMap, "match NodeMap")
		assert.Equal(t, want.CLevel, got.CLevel, "match CLevel")
		assert.Equal(t, want.Level, got.Level, "match Level")
	}

	nr := newFakeNodeResolver([]string{"A", "B", "C"})
	r := resolver{
		nodeResolver: nr,
		Class:        "C",
		NodeName:     "A",
		Schema:       newFakeShardingState("A", ss, nr),
	}
	t.Run("ShardingState", func(t *testing.T) {
		_, err := r.State("Sx", One, "")
		assert.NotNil(t, err)
		assert.Contains(t, err.Error(), "sharding state")
	})
	t.Run("ALL", func(t *testing.T) {
		r := resolver{
			nodeResolver: nr,
			Class:        "C",
			NodeName:     "B",
			Schema:       newFakeShardingState("B", ss, nr),
		}
		got, err := r.State("S1", All, "")
		assert.Nil(t, err)
		m := make(map[string]string, len(ss["S1"]))
		for _, k := range ss["S1"] {
			m[k] = nr.hosts[k]
		}
		want := rState{All, len(ss["S1"]), ss["S1"], m}
		assertSameHosts(want, got, "B")
	})

	t.Run("ALLWithDirectCandidate", func(t *testing.T) {
		got, err := r.State("S1", All, "B")
		assert.Nil(t, err)
		m := make(map[string]string, len(ss["S1"]))
		for _, k := range ss["S1"] {
			m[k] = nr.hosts[k]
		}
		want := rState{All, len(ss["S1"]), ss["S1"], m}
		assertSameHosts(want, got, "B")
	})
	t.Run("Quorum", func(t *testing.T) {
		got, err := r.State("S3", Quorum, "")
		assert.Nil(t, err)

		m := make(map[string]string, len(ss["S1"]))
		for _, k := range ss["S3"] {
			m[k] = nr.hosts[k]
		}
		want := rState{Quorum, len(ss["S1"]), ss["S1"], m} // ss["S2"]}
		assertSameHosts(want, got, "A")
		_, err = got.ConsistencyLevel(All)
		assert.ErrorIs(t, err, errUnresolvedName)
		_, err = got.ConsistencyLevel(Quorum)
		assert.Nil(t, err)
		_, err = got.ConsistencyLevel(One)
		assert.Nil(t, err)
	})
	t.Run("NoQuorum", func(t *testing.T) {
		got, err := r.State("S5", Quorum, "")
		assert.ErrorIs(t, err, errUnresolvedName)
		m := make(map[string]string, len(ss["S1"]))
		for _, k := range ss["S5"] {
			m[k] = nr.hosts[k]
		}
		want := rState{Quorum, 0, ss["S1"], m} // ss["S4"]}
		assertSameHosts(want, got, "A")

		_, err = got.ConsistencyLevel(All)
		assert.ErrorIs(t, err, errUnresolvedName)
		_, err = got.ConsistencyLevel(Quorum)
		assert.ErrorIs(t, err, errUnresolvedName)
		_, err = got.ConsistencyLevel(One)
		assert.Nil(t, err)
	})
}