|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// |
| 3 | +// The OpenSearch Contributors require contributions made to |
| 4 | +// this file be licensed under the Apache-2.0 license or a |
| 5 | +// compatible open source license. |
| 6 | + |
| 7 | +package opensearchtransport |
| 8 | + |
| 9 | +import ( |
| 10 | + "net/url" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +// TestAvailableForRouting covers the per-connection predicate that decides |
| 17 | +// whether a connection may satisfy a policy's "has anything to serve" check. |
| 18 | +// Seeds are always available; discovered connections are available only once |
| 19 | +// they have been verified (lcNeedsHardware cleared). |
| 20 | +func TestAvailableForRouting(t *testing.T) { |
| 21 | + newConn := func(seed bool, lc connLifecycle) *Connection { |
| 22 | + c := &Connection{URL: &url.URL{Scheme: "http", Host: "n:9200"}, seed: seed} |
| 23 | + c.state.Store(int64(newConnState(lc))) |
| 24 | + return c |
| 25 | + } |
| 26 | + |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + conn *Connection |
| 30 | + want bool |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "seed dead and never verified is still available", |
| 34 | + conn: newConn(true, lcDead|lcNeedsWarmup|lcNeedsHardware), |
| 35 | + want: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "seed active is available", |
| 39 | + conn: newConn(true, lcActive), |
| 40 | + want: true, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "discovered never verified is not available", |
| 44 | + conn: newConn(false, lcDead|lcNeedsWarmup|lcNeedsHardware), |
| 45 | + want: false, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "discovered verified (hardware cleared) is available", |
| 49 | + conn: newConn(false, lcDead|lcNeedsWarmup), |
| 50 | + want: true, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "zero-value (non-seed, no hardware bit) defaults to available", |
| 54 | + conn: &Connection{URL: &url.URL{Scheme: "http", Host: "n:9200"}}, |
| 55 | + want: true, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + for _, tt := range tests { |
| 60 | + t.Run(tt.name, func(t *testing.T) { |
| 61 | + require.Equal(t, tt.want, tt.conn.availableForRouting()) |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// TestHasAvailableConnsWithLock covers the pool-level predicate that policies |
| 67 | +// use to set their enabled bit. A pool whose only dead connections are |
| 68 | +// never-verified discovered nodes must report "no available connections" so the |
| 69 | +// request cascades to the seed fallback. |
| 70 | +func TestHasAvailableConnsWithLock(t *testing.T) { |
| 71 | + seedURL := &url.URL{Scheme: "http", Host: "seed:9200"} |
| 72 | + discURL := &url.URL{Scheme: "http", Host: "10.0.0.1:9200"} |
| 73 | + |
| 74 | + deadDiscovered := func() *Connection { |
| 75 | + c := &Connection{URL: discURL, URLString: discURL.String()} |
| 76 | + c.state.Store(int64(newConnState(lcDead | lcNeedsWarmup | lcNeedsHardware))) |
| 77 | + return c |
| 78 | + } |
| 79 | + deadSeed := func() *Connection { |
| 80 | + c := &Connection{URL: seedURL, URLString: seedURL.String(), seed: true} |
| 81 | + c.state.Store(int64(newConnState(lcDead | lcNeedsWarmup | lcNeedsHardware))) |
| 82 | + return c |
| 83 | + } |
| 84 | + verifiedDiscovered := func() *Connection { |
| 85 | + c := &Connection{URL: discURL, URLString: discURL.String()} |
| 86 | + c.state.Store(int64(newConnState(lcDead | lcNeedsWarmup))) // hardware cleared |
| 87 | + return c |
| 88 | + } |
| 89 | + |
| 90 | + t.Run("only never-verified discovered dead conns -> not available", func(t *testing.T) { |
| 91 | + cp := &multiServerPool{name: "test"} |
| 92 | + cp.mu.dead = []*Connection{deadDiscovered(), deadDiscovered()} |
| 93 | + require.False(t, cp.hasAvailableConnsWithLock()) |
| 94 | + }) |
| 95 | + |
| 96 | + t.Run("a dead seed keeps the pool available", func(t *testing.T) { |
| 97 | + cp := &multiServerPool{name: "test"} |
| 98 | + cp.mu.dead = []*Connection{deadDiscovered(), deadSeed()} |
| 99 | + require.True(t, cp.hasAvailableConnsWithLock()) |
| 100 | + }) |
| 101 | + |
| 102 | + t.Run("a verified discovered dead conn keeps the pool available", func(t *testing.T) { |
| 103 | + cp := &multiServerPool{name: "test"} |
| 104 | + cp.mu.dead = []*Connection{deadDiscovered(), verifiedDiscovered()} |
| 105 | + require.True(t, cp.hasAvailableConnsWithLock()) |
| 106 | + }) |
| 107 | + |
| 108 | + t.Run("any ready conn keeps the pool available", func(t *testing.T) { |
| 109 | + cp := &multiServerPool{name: "test"} |
| 110 | + ready := &Connection{URL: discURL, URLString: discURL.String()} |
| 111 | + ready.state.Store(int64(newConnState(lcActive))) |
| 112 | + cp.mu.ready = []*Connection{ready} |
| 113 | + require.True(t, cp.hasAvailableConnsWithLock()) |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("empty pool is not available", func(t *testing.T) { |
| 117 | + cp := &multiServerPool{name: "test"} |
| 118 | + require.False(t, cp.hasAvailableConnsWithLock()) |
| 119 | + }) |
| 120 | +} |
0 commit comments