|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestPrimaryOnlyTables(t *testing.T) { |
| 13 | + pool := GetPool() |
| 14 | + defer pool.Close() |
| 15 | + |
| 16 | + _, err := pool.Exec(context.Background(), `CREATE TABLE IF NOT EXISTS lb_plugin_primary_only ( |
| 17 | + id BIGINT, |
| 18 | + data VARCHAR |
| 19 | + )`) |
| 20 | + assert.NoError(t, err) |
| 21 | + defer pool.Exec(context.Background(), "DROP TABLE IF EXISTS lb_plugin_primary_only") |
| 22 | + |
| 23 | + time.Sleep(2 * time.Second) |
| 24 | + |
| 25 | + ResetStats() |
| 26 | + |
| 27 | + for i := range 50 { |
| 28 | + _, err = pool.Exec(context.Background(), "SELECT $1::bigint FROM lb_plugin_primary_only", int64(i)) |
| 29 | + assert.NoError(t, err) |
| 30 | + } |
| 31 | + |
| 32 | + primaryCalls := LoadStatsForPrimary("lb_plugin_primary_only") |
| 33 | + assert.Equal(t, int64(50), primaryCalls.Calls) |
| 34 | +} |
| 35 | + |
| 36 | +func TestPrimaryOnlyTablesExplain(t *testing.T) { |
| 37 | + pool := GetPool() |
| 38 | + defer pool.Close() |
| 39 | + |
| 40 | + _, err := pool.Exec(context.Background(), `CREATE TABLE IF NOT EXISTS lb_plugin_primary_only ( |
| 41 | + id BIGINT, |
| 42 | + data VARCHAR |
| 43 | + )`) |
| 44 | + assert.NoError(t, err) |
| 45 | + defer pool.Exec(context.Background(), "DROP TABLE IF EXISTS lb_plugin_primary_only") |
| 46 | + |
| 47 | + time.Sleep(2 * time.Second) |
| 48 | + |
| 49 | + rows, err := pool.Query(context.Background(), "EXPLAIN SELECT * FROM lb_plugin_primary_only") |
| 50 | + assert.NoError(t, err) |
| 51 | + |
| 52 | + var explainLines []string |
| 53 | + foundPluginAnnotation := false |
| 54 | + for rows.Next() { |
| 55 | + var line string |
| 56 | + err = rows.Scan(&line) |
| 57 | + assert.NoError(t, err) |
| 58 | + explainLines = append(explainLines, line) |
| 59 | + |
| 60 | + if strings.Contains(line, "plugin pgdog_primary_only_tables adjusted routing role=primary") { |
| 61 | + foundPluginAnnotation = true |
| 62 | + } |
| 63 | + } |
| 64 | + rows.Close() |
| 65 | + |
| 66 | + t.Logf("EXPLAIN output:\n%s", strings.Join(explainLines, "\n")) |
| 67 | + assert.True(t, foundPluginAnnotation, "EXPLAIN output should contain plugin routing annotation") |
| 68 | +} |
0 commit comments