|
| 1 | +package corazawaf |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +// TestTransactionPoolCacheIsolation verifies that each transaction gets its own cache |
| 10 | +func TestTransactionPoolCacheIsolation(t *testing.T) { |
| 11 | + waf := NewWAF() |
| 12 | + |
| 13 | + // Create two transactions |
| 14 | + tx1, tx2 := waf.NewTransaction(), waf.NewTransaction() |
| 15 | + assert.Empty(t, tx1.transformationCache) |
| 16 | + assert.Empty(t, tx2.transformationCache) |
| 17 | + // put some value in the transformation cache for tx1 |
| 18 | + tk1 := transformationKey{transformationsID: 1} |
| 19 | + tx1.transformationCache[tk1] = &transformationValue{arg: "bla"} |
| 20 | + assert.NotEmpty(t, tx1.transformationCache) |
| 21 | + |
| 22 | + t.Logf("tx1.transformationCache pointer is %p", tx1.transformationCache) |
| 23 | + t.Logf("tx2.transformationCache pointer is %p", tx2.transformationCache) |
| 24 | + // close both transactions |
| 25 | + if err := tx1.Close(); err != nil { |
| 26 | + t.Fatalf("Failed to close tx1: %s", err.Error()) |
| 27 | + } |
| 28 | + if err := tx2.Close(); err != nil { |
| 29 | + t.Fatalf("Failed to close tx2: %s", err.Error()) |
| 30 | + } |
| 31 | + |
| 32 | + tx3, tx4 := waf.NewTransaction(), waf.NewTransaction() |
| 33 | + // Compare map pointers using reflection to ensure they are different instances |
| 34 | + assert.Emptyf(t, tx3.transformationCache, |
| 35 | + "tx3.transformationCache should be empty for new transactions, but it is not, pointer is %p", |
| 36 | + tx3.transformationCache) |
| 37 | + assert.Emptyf(t, tx4.transformationCache, |
| 38 | + "tx4.transformationCache should be empty for new transactions, but it is not, pointer is %p", |
| 39 | + tx4.transformationCache) |
| 40 | +} |
0 commit comments