|
| 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 the first transactions |
| 14 | + tx1 := waf.NewTransaction() |
| 15 | + assert.Empty(t, tx1.transformationCache) |
| 16 | + // put some value in the transformation cache for tx1 |
| 17 | + tk1 := transformationKey{transformationsID: 1} |
| 18 | + tx1.transformationCache[tk1] = &transformationValue{arg: "bla"} |
| 19 | + assert.NotEmpty(t, tx1.transformationCache) |
| 20 | + |
| 21 | + t.Logf("tx1.transformationCache pointer is %p", tx1.transformationCache) |
| 22 | + // close both transactions |
| 23 | + if err := tx1.Close(); err != nil { |
| 24 | + t.Fatalf("Failed to close tx1: %s", err.Error()) |
| 25 | + } |
| 26 | + |
| 27 | + // Create the second transaction and save its pointer to transformationCache |
| 28 | + // Create two transactions |
| 29 | + tx2 := waf.NewTransaction() |
| 30 | + assert.Empty(t, tx2.transformationCache) |
| 31 | + // put some value in the transformation cache for tx1 |
| 32 | + |
| 33 | + t.Logf("tx2.transformationCache pointer is %p", tx2.transformationCache) |
| 34 | + if err := tx2.Close(); err != nil { |
| 35 | + t.Fatalf("Failed to close tx2: %s", err.Error()) |
| 36 | + } |
| 37 | + |
| 38 | + tx3, tx4 := waf.NewTransaction(), waf.NewTransaction() |
| 39 | + //tx1.variables |
| 40 | + |
| 41 | + t.Logf("\ntx1.transformationCache pointer is %p\n"+ |
| 42 | + "tx2.tranformactionCache pointer is %p\n"+ |
| 43 | + "tx3.tranformactionCache pointer is %p\n"+ |
| 44 | + "tx4.tranformactionCache pointer is %p\n", |
| 45 | + tx1.transformationCache, |
| 46 | + tx2.transformationCache, |
| 47 | + tx3.transformationCache, |
| 48 | + tx4.transformationCache) |
| 49 | + |
| 50 | + assert.Empty(t, tx3.transformationCache) |
| 51 | + assert.Empty(t, tx4.transformationCache) |
| 52 | +} |
0 commit comments