(t *testing.T)
| 109 | } |
| 110 | |
| 111 | func TestGetHost(t *testing.T) { |
| 112 | c := &cluster{ |
| 113 | name: "default", |
| 114 | replicas: []*replica{{}}, |
| 115 | } |
| 116 | r := c.replicas[0] |
| 117 | r.cluster = c |
| 118 | r.hosts = []*topology.Node{ |
| 119 | topology.NewNode(&url.URL{Host: "127.0.0.1"}, nil, "", r.name, topology.WithDefaultActiveState(true)), |
| 120 | topology.NewNode(&url.URL{Host: "127.0.0.2"}, nil, "", r.name, topology.WithDefaultActiveState(true)), |
| 121 | topology.NewNode(&url.URL{Host: "127.0.0.3"}, nil, "", r.name, topology.WithDefaultActiveState(true)), |
| 122 | } |
| 123 | |
| 124 | // step | expected | hosts running queries |
| 125 | // 1 | 127.0.0.2 | 0, 1, 0 |
| 126 | // 2 | 127.0.0.3 | 0, 1, 1 |
| 127 | // 3 | 127.0.0.1 | 1, 1, 1 |
| 128 | // 4 | 127.0.0.2 | 1, 2, 2 |
| 129 | // 5 | 127.0.0.1 | 2, 2, 2 |
| 130 | // 6 | 127.0.0.1 | 3, 7, 2 // 2nd is penalized for `penaltySize` |
| 131 | // 7 | 127.0.0.1 | 3, 7, 3 |
| 132 | |
| 133 | // step: 1 |
| 134 | h := c.getHost() |
| 135 | expected := "127.0.0.2" |
| 136 | if h.Host() != expected { |
| 137 | t.Fatalf("got host %q; expected %q", h.Host(), expected) |
| 138 | } |
| 139 | h.IncrementConnections() |
| 140 | |
| 141 | // step: 2 |
| 142 | h = c.getHost() |
| 143 | expected = "127.0.0.3" |
| 144 | if h.Host() != expected { |
| 145 | t.Fatalf("got host %q; expected %q", h.Host(), expected) |
| 146 | } |
| 147 | h.IncrementConnections() |
| 148 | |
| 149 | // step: 3 |
| 150 | h = c.getHost() |
| 151 | expected = "127.0.0.1" |
| 152 | if h.Host() != expected { |
| 153 | t.Fatalf("got host %q; expected %q", h.Host(), expected) |
| 154 | } |
| 155 | h.IncrementConnections() |
| 156 | |
| 157 | // step: 4 |
| 158 | h = c.getHost() |
| 159 | expected = "127.0.0.2" |
| 160 | if h.Host() != expected { |
| 161 | t.Fatalf("got host %q; expected %q", h.Host(), expected) |
| 162 | } |
| 163 | h.IncrementConnections() |
| 164 | |
| 165 | // inc last host to get least-loaded 1st host |
| 166 | r.hosts[2].IncrementConnections() |
| 167 | |
| 168 | // step: 5 |
nothing calls this directly
no test coverage detected