|
| 1 | +// Copyright 2026 GoSQLX Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package metrics_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/ajitpratap0/GoSQLX/pkg/metrics" |
| 21 | +) |
| 22 | + |
| 23 | +func TestPoolStats_ReturnsNonNilResult(t *testing.T) { |
| 24 | + stats := metrics.GetPoolStats() |
| 25 | + if stats == nil { |
| 26 | + t.Fatal("GetPoolStats returned nil") |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestPoolStats_HasTokenizerPool(t *testing.T) { |
| 31 | + stats := metrics.GetPoolStats() |
| 32 | + if _, ok := stats["tokenizer"]; !ok { |
| 33 | + t.Error("expected 'tokenizer' key in pool stats") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestPoolStats_HasParserPool(t *testing.T) { |
| 38 | + stats := metrics.GetPoolStats() |
| 39 | + if _, ok := stats["parser"]; !ok { |
| 40 | + t.Error("expected 'parser' key in pool stats") |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestPoolStats_HasASTPool(t *testing.T) { |
| 45 | + stats := metrics.GetPoolStats() |
| 46 | + if _, ok := stats["ast"]; !ok { |
| 47 | + t.Error("expected 'ast' key in pool stats") |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestPoolStats_RecordGet(t *testing.T) { |
| 52 | + metrics.ResetPoolStats() |
| 53 | + |
| 54 | + _ = metrics.RecordNamedPoolGet("tokenizer") |
| 55 | + _ = metrics.RecordNamedPoolPut("tokenizer") |
| 56 | + |
| 57 | + stats := metrics.GetPoolStats() |
| 58 | + ts, ok := stats["tokenizer"] |
| 59 | + if !ok { |
| 60 | + t.Fatal("missing tokenizer key after reset+record") |
| 61 | + } |
| 62 | + if ts.Gets < 1 { |
| 63 | + t.Errorf("expected Gets >= 1 after RecordNamedPoolGet, got %d", ts.Gets) |
| 64 | + } |
| 65 | + if ts.Puts < 1 { |
| 66 | + t.Errorf("expected Puts >= 1 after RecordNamedPoolPut, got %d", ts.Puts) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestPoolStats_ActiveCalculation(t *testing.T) { |
| 71 | + metrics.ResetPoolStats() |
| 72 | + |
| 73 | + metrics.RecordNamedPoolGet("parser") |
| 74 | + metrics.RecordNamedPoolGet("parser") |
| 75 | + metrics.RecordNamedPoolPut("parser") |
| 76 | + |
| 77 | + stats := metrics.GetPoolStats() |
| 78 | + ps := stats["parser"] |
| 79 | + if ps.Gets != 2 { |
| 80 | + t.Errorf("expected Gets=2, got %d", ps.Gets) |
| 81 | + } |
| 82 | + if ps.Puts != 1 { |
| 83 | + t.Errorf("expected Puts=1, got %d", ps.Puts) |
| 84 | + } |
| 85 | + if ps.Active() != 1 { |
| 86 | + t.Errorf("expected Active()=1, got %d", ps.Active()) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestPoolStats_ResetClearsCounters(t *testing.T) { |
| 91 | + metrics.RecordNamedPoolGet("ast") |
| 92 | + metrics.ResetPoolStats() |
| 93 | + |
| 94 | + stats := metrics.GetPoolStats() |
| 95 | + // After reset, standard pools are recreated with zero values |
| 96 | + as := stats["ast"] |
| 97 | + if as.Gets != 0 { |
| 98 | + t.Errorf("expected Gets=0 after reset, got %d", as.Gets) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestPoolStats_ConcurrentSafe(t *testing.T) { |
| 103 | + metrics.ResetPoolStats() |
| 104 | + |
| 105 | + done := make(chan struct{}) |
| 106 | + for i := 0; i < 100; i++ { |
| 107 | + go func() { |
| 108 | + metrics.RecordNamedPoolGet("tokenizer") |
| 109 | + metrics.RecordNamedPoolPut("tokenizer") |
| 110 | + done <- struct{}{} |
| 111 | + }() |
| 112 | + } |
| 113 | + for i := 0; i < 100; i++ { |
| 114 | + <-done |
| 115 | + } |
| 116 | + |
| 117 | + stats := metrics.GetPoolStats() |
| 118 | + ts := stats["tokenizer"] |
| 119 | + if ts.Gets != 100 { |
| 120 | + t.Errorf("expected Gets=100, got %d", ts.Gets) |
| 121 | + } |
| 122 | + if ts.Puts != 100 { |
| 123 | + t.Errorf("expected Puts=100, got %d", ts.Puts) |
| 124 | + } |
| 125 | +} |
0 commit comments