mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-12 00:45:31 +00:00
Compare commits
@@ -0,0 +1,181 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Benchmark tiered buffer pools vs single pool
|
||||
func BenchmarkBufferPools(b *testing.B) {
|
||||
// Test data of different sizes
|
||||
smallData := make([]byte, 512)
|
||||
mediumData := make([]byte, 2048)
|
||||
largeData := make([]byte, 8192)
|
||||
|
||||
for i := range smallData {
|
||||
smallData[i] = byte(i % 256)
|
||||
}
|
||||
for i := range mediumData {
|
||||
mediumData[i] = byte(i % 256)
|
||||
}
|
||||
for i := range largeData {
|
||||
largeData[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
b.Run("SmallBuffer_GetPut", func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
buf := getBuffer(len(smallData))
|
||||
buf.Write(smallData)
|
||||
putBuffer(buf)
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("MediumBuffer_GetPut", func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
buf := getBuffer(len(mediumData))
|
||||
buf.Write(mediumData)
|
||||
putBuffer(buf)
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("LargeBuffer_GetPut", func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
buf := getBuffer(len(largeData))
|
||||
buf.Write(largeData)
|
||||
putBuffer(buf)
|
||||
}
|
||||
})
|
||||
|
||||
// Compare with old single pool approach
|
||||
b.Run("SinglePool_Small", func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
buf := bufferPool.Get().(*bytes.Buffer)
|
||||
buf.Reset()
|
||||
buf.Write(smallData)
|
||||
bufferPool.Put(buf)
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("SinglePool_Medium", func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
buf := bufferPool.Get().(*bytes.Buffer)
|
||||
buf.Reset()
|
||||
buf.Write(mediumData)
|
||||
bufferPool.Put(buf)
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("SinglePool_Large", func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
buf := bufferPool.Get().(*bytes.Buffer)
|
||||
buf.Reset()
|
||||
buf.Write(largeData)
|
||||
bufferPool.Put(buf)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Benchmark buffer allocation patterns
|
||||
func BenchmarkBufferAllocation(b *testing.B) {
|
||||
b.Run("TieredPools_Mixed", func(b *testing.B) {
|
||||
sizes := []int{256, 512, 1024, 2048, 4096, 8192, 16384}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
size := sizes[i%len(sizes)]
|
||||
buf := getBuffer(size)
|
||||
data := make([]byte, size)
|
||||
buf.Write(data)
|
||||
putBuffer(buf)
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("DirectAllocation_Mixed", func(b *testing.B) {
|
||||
sizes := []int{256, 512, 1024, 2048, 4096, 8192, 16384}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
size := sizes[i%len(sizes)]
|
||||
buf := bytes.NewBuffer(make([]byte, 0, size))
|
||||
data := make([]byte, size)
|
||||
buf.Write(data)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Test buffer pool correctness
|
||||
func TestBufferPoolCorrectness(t *testing.T) {
|
||||
testData := []byte("Hello, World! This is test data for buffer pool testing.")
|
||||
|
||||
// Test small buffer
|
||||
buf := getBuffer(len(testData))
|
||||
buf.Write(testData)
|
||||
if buf.Len() != len(testData) {
|
||||
t.Errorf("Buffer length mismatch: expected %d, got %d", len(testData), buf.Len())
|
||||
}
|
||||
if !bytes.Equal(buf.Bytes(), testData) {
|
||||
t.Error("Buffer content mismatch")
|
||||
}
|
||||
putBuffer(buf)
|
||||
|
||||
// Test that buffer is properly reset when returned to pool
|
||||
buf2 := getBuffer(10)
|
||||
if buf2.Len() != 0 {
|
||||
t.Error("Buffer not properly reset")
|
||||
}
|
||||
putBuffer(buf2)
|
||||
|
||||
// Test large buffer
|
||||
largeData := make([]byte, 10000)
|
||||
for i := range largeData {
|
||||
largeData[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
largeBuf := getBuffer(len(largeData))
|
||||
largeBuf.Write(largeData)
|
||||
if largeBuf.Len() != len(largeData) {
|
||||
t.Errorf("Large buffer length mismatch: expected %d, got %d", len(largeData), largeBuf.Len())
|
||||
}
|
||||
putBuffer(largeBuf)
|
||||
}
|
||||
|
||||
// Test buffer pool size selection
|
||||
func TestBufferPoolSizeSelection(t *testing.T) {
|
||||
testCases := []struct {
|
||||
size int
|
||||
expectedPool string
|
||||
}{
|
||||
{100, "small"},
|
||||
{1024, "small"},
|
||||
{1025, "medium"},
|
||||
{4096, "medium"},
|
||||
{4097, "large"},
|
||||
{16384, "large"},
|
||||
{20000, "large"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
buf := getBuffer(tc.size)
|
||||
capacity := buf.Cap()
|
||||
|
||||
var poolType string
|
||||
switch {
|
||||
case capacity <= 1024:
|
||||
poolType = "small"
|
||||
case capacity <= 4096:
|
||||
poolType = "medium"
|
||||
default:
|
||||
poolType = "large"
|
||||
}
|
||||
|
||||
if poolType != tc.expectedPool {
|
||||
t.Errorf("Size %d: expected %s pool, got %s pool (capacity: %d)",
|
||||
tc.size, tc.expectedPool, poolType, capacity)
|
||||
}
|
||||
putBuffer(buf)
|
||||
}
|
||||
}
|
||||
Vendored
+168
-46
@@ -3,27 +3,48 @@ package libpack_cache
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"hash/fnv"
|
||||
"io"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CacheEntry struct {
|
||||
ExpiresAt time.Time
|
||||
Value []byte
|
||||
ExpiresAt time.Time
|
||||
Value []byte
|
||||
IsCompressed bool
|
||||
}
|
||||
|
||||
const shardCount = 256 // Must be power of 2
|
||||
|
||||
type shard struct {
|
||||
entries map[string]CacheEntry
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
compressPool sync.Pool
|
||||
decompressPool sync.Pool
|
||||
entries sync.Map
|
||||
shards [shardCount]*shard
|
||||
globalTTL time.Duration
|
||||
sync.RWMutex
|
||||
cleanupChan chan struct{}
|
||||
stopChan chan struct{}
|
||||
stopped bool
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// getShard returns the appropriate shard for a given key
|
||||
func (c *Cache) getShard(key string) *shard {
|
||||
hash := fnv.New32a()
|
||||
hash.Write([]byte(key))
|
||||
return c.shards[hash.Sum32()%shardCount]
|
||||
}
|
||||
|
||||
func New(globalTTL time.Duration) *Cache {
|
||||
cache := &Cache{
|
||||
globalTTL: globalTTL,
|
||||
globalTTL: globalTTL,
|
||||
cleanupChan: make(chan struct{}, 1),
|
||||
stopChan: make(chan struct{}),
|
||||
compressPool: sync.Pool{
|
||||
New: func() interface{} {
|
||||
w := gzip.NewWriter(nil)
|
||||
@@ -32,81 +53,182 @@ func New(globalTTL time.Duration) *Cache {
|
||||
},
|
||||
decompressPool: sync.Pool{
|
||||
New: func() interface{} {
|
||||
// Ensure that new is returning a new reader initialized with an empty byte buffer
|
||||
r, _ := gzip.NewReader(bytes.NewReader([]byte{}))
|
||||
return r
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
go cache.cleanupRoutine(globalTTL)
|
||||
// Initialize shards
|
||||
for i := 0; i < shardCount; i++ {
|
||||
cache.shards[i] = &shard{
|
||||
entries: make(map[string]CacheEntry),
|
||||
}
|
||||
}
|
||||
|
||||
go cache.lazyCleanupWorker()
|
||||
go cache.periodicCleanupRoutine(globalTTL)
|
||||
return cache
|
||||
}
|
||||
|
||||
func (c *Cache) cleanupRoutine(globalTTL time.Duration) {
|
||||
ticker := time.NewTicker(globalTTL / 2)
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
c.CleanExpiredEntries()
|
||||
func (c *Cache) lazyCleanupWorker() {
|
||||
for {
|
||||
select {
|
||||
case <-c.cleanupChan:
|
||||
c.mu.RLock()
|
||||
if c.stopped {
|
||||
c.mu.RUnlock()
|
||||
return
|
||||
}
|
||||
c.mu.RUnlock()
|
||||
c.CleanExpiredEntries()
|
||||
case <-c.stopChan:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
func (c *Cache) Set(key string, value []byte, ttl time.Duration) {
|
||||
c.Lock() // use the lock
|
||||
defer c.Unlock()
|
||||
|
||||
expiresAt := time.Now().Add(ttl)
|
||||
func (c *Cache) periodicCleanupRoutine(globalTTL time.Duration) {
|
||||
// Use a shorter cleanup interval for better responsiveness, but not less than 1 second
|
||||
cleanupInterval := globalTTL / 4
|
||||
if cleanupInterval < time.Second {
|
||||
cleanupInterval = time.Second
|
||||
}
|
||||
|
||||
compressedValue, err := c.compress(value)
|
||||
if err != nil {
|
||||
ticker := time.NewTicker(cleanupInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
c.mu.RLock()
|
||||
if c.stopped {
|
||||
c.mu.RUnlock()
|
||||
return
|
||||
}
|
||||
c.mu.RUnlock()
|
||||
c.CleanExpiredEntries()
|
||||
case <-c.stopChan:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) triggerLazyCleanup() {
|
||||
c.mu.RLock()
|
||||
if c.stopped {
|
||||
c.mu.RUnlock()
|
||||
return
|
||||
}
|
||||
c.mu.RUnlock()
|
||||
|
||||
entry := CacheEntry{
|
||||
Value: compressedValue,
|
||||
ExpiresAt: expiresAt,
|
||||
select {
|
||||
case c.cleanupChan <- struct{}{}:
|
||||
// Cleanup triggered
|
||||
default:
|
||||
// Cleanup already pending, skip
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) Stop() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if c.stopped {
|
||||
return // Already stopped
|
||||
}
|
||||
|
||||
c.stopped = true
|
||||
close(c.stopChan)
|
||||
}
|
||||
|
||||
func (c *Cache) Set(key string, value []byte, ttl time.Duration) {
|
||||
shard := c.getShard(key)
|
||||
shard.Lock()
|
||||
defer shard.Unlock()
|
||||
|
||||
const compressionThreshold = 1024 // 1KB threshold
|
||||
var finalValue []byte
|
||||
var isCompressed bool
|
||||
|
||||
if len(value) >= compressionThreshold {
|
||||
compressedValue, err := c.compress(value)
|
||||
if err != nil {
|
||||
// If compression fails, store uncompressed
|
||||
finalValue = value
|
||||
isCompressed = false
|
||||
} else {
|
||||
// Only use compression if it actually reduces size
|
||||
if len(compressedValue) < len(value) {
|
||||
finalValue = compressedValue
|
||||
isCompressed = true
|
||||
} else {
|
||||
finalValue = value
|
||||
isCompressed = false
|
||||
}
|
||||
}
|
||||
} else {
|
||||
finalValue = value
|
||||
isCompressed = false
|
||||
}
|
||||
|
||||
shard.entries[key] = CacheEntry{
|
||||
Value: finalValue,
|
||||
ExpiresAt: time.Now().Add(ttl),
|
||||
IsCompressed: isCompressed,
|
||||
}
|
||||
c.entries.Store(key, entry)
|
||||
}
|
||||
|
||||
func (c *Cache) Get(key string) ([]byte, bool) {
|
||||
c.RLock() // use the read lock
|
||||
defer c.RUnlock()
|
||||
|
||||
entry, ok := c.entries.Load(key)
|
||||
if !ok || entry.(CacheEntry).ExpiresAt.Before(time.Now()) {
|
||||
shard := c.getShard(key)
|
||||
shard.RLock()
|
||||
entry, ok := shard.entries[key]
|
||||
if !ok {
|
||||
shard.RUnlock()
|
||||
return nil, false
|
||||
}
|
||||
compressedValue := entry.(CacheEntry).Value
|
||||
value, err := c.decompress(compressedValue)
|
||||
if err != nil {
|
||||
|
||||
if entry.ExpiresAt.Before(time.Now()) {
|
||||
shard.RUnlock()
|
||||
// Trigger lazy cleanup instead of immediate deletion
|
||||
c.triggerLazyCleanup()
|
||||
return nil, false
|
||||
}
|
||||
shard.RUnlock()
|
||||
|
||||
var value []byte
|
||||
var err error
|
||||
|
||||
if entry.IsCompressed {
|
||||
value, err = c.decompress(entry.Value)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
} else {
|
||||
value = entry.Value
|
||||
}
|
||||
|
||||
return value, true
|
||||
}
|
||||
|
||||
func (c *Cache) Delete(key string) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
_, ok := c.entries.Load(key)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
c.entries.Delete(key)
|
||||
shard := c.getShard(key)
|
||||
shard.Lock()
|
||||
delete(shard.entries, key)
|
||||
shard.Unlock()
|
||||
}
|
||||
|
||||
func (c *Cache) CleanExpiredEntries() {
|
||||
now := time.Now()
|
||||
c.entries.Range(func(key, value interface{}) bool {
|
||||
entry := value.(CacheEntry)
|
||||
if entry.ExpiresAt.Before(now) {
|
||||
c.entries.Delete(key)
|
||||
for _, shard := range c.shards {
|
||||
shard.Lock()
|
||||
for key, entry := range shard.entries {
|
||||
if entry.ExpiresAt.Before(now) {
|
||||
delete(shard.entries, key)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
shard.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) compress(data []byte) ([]byte, error) {
|
||||
|
||||
Vendored
+11
-7
@@ -8,22 +8,24 @@ import (
|
||||
// Assume that New function initializes the cache and it is defined somewhere in the libpack_cache package.
|
||||
|
||||
func BenchmarkCacheSet(b *testing.B) {
|
||||
cache := New(30 * time.Second) // Initializing the cache with a TTL of 30 seconds
|
||||
cache := New(5 * time.Second) // Use shorter TTL for tests
|
||||
defer cache.Stop()
|
||||
key := "benchmark-key"
|
||||
value := []byte("benchmark-value")
|
||||
|
||||
b.ResetTimer() // Reset the timer to exclude the setup time from the benchmark
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
cache.Set(key, value, 5*time.Second)
|
||||
cache.Set(key, value, 2*time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCacheGet(b *testing.B) {
|
||||
cache := New(30 * time.Second) // Initializing the cache
|
||||
cache := New(5 * time.Second) // Use shorter TTL for tests
|
||||
defer cache.Stop()
|
||||
key := "benchmark-key"
|
||||
value := []byte("benchmark-value")
|
||||
cache.Set(key, value, 5*time.Second) // Pre-set a value to retrieve
|
||||
cache.Set(key, value, 2*time.Second) // Pre-set a value to retrieve
|
||||
|
||||
b.ResetTimer() // Start timing
|
||||
|
||||
@@ -33,14 +35,16 @@ func BenchmarkCacheGet(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkCacheExpire(b *testing.B) {
|
||||
key := "benchmark-expire-key"
|
||||
cache := New(5 * time.Second) // Use shorter TTL for tests and reuse cache
|
||||
defer cache.Stop()
|
||||
value := []byte("benchmark-value")
|
||||
ttl := 5 * time.Millisecond // Setting a short TTL for quick expiration
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
cache := New(30 * time.Second)
|
||||
key := "benchmark-expire-key-" + string(rune(i)) // Use unique keys
|
||||
cache.Set(key, value, ttl)
|
||||
time.Sleep(ttl) // Wait for the key to expire
|
||||
time.Sleep(ttl + time.Millisecond) // Wait for the key to expire with buffer
|
||||
_, _ = cache.Get(key)
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+215
-8
@@ -21,12 +21,15 @@ func TestCachingTestSuite(t *testing.T) {
|
||||
func (suite *CacheTestSuite) Test_New() {
|
||||
suite.T().Run("should return a new cache", func(t *testing.T) {
|
||||
cache := New(2 * time.Second)
|
||||
defer cache.Stop()
|
||||
suite.NotNil(cache)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *CacheTestSuite) Test_CacheUse() {
|
||||
cache := New(30 * time.Second)
|
||||
cache := New(5 * time.Second)
|
||||
defer cache.Stop()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cache_value string
|
||||
@@ -42,7 +45,7 @@ func (suite *CacheTestSuite) Test_CacheUse() {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
suite.T().Run(tt.name, func(t *testing.T) {
|
||||
cache.Set(tt.name, []byte(tt.name), 5*time.Second)
|
||||
cache.Set(tt.name, []byte(tt.name), 2*time.Second)
|
||||
c, ok := cache.Get(tt.name)
|
||||
suite.Equal(true, ok)
|
||||
suite.Equal(tt.name, string(c))
|
||||
@@ -51,7 +54,9 @@ func (suite *CacheTestSuite) Test_CacheUse() {
|
||||
}
|
||||
|
||||
func (suite *CacheTestSuite) Test_CacheDelete() {
|
||||
cache := New(30 * time.Second)
|
||||
cache := New(5 * time.Second)
|
||||
defer cache.Stop()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cache_value string
|
||||
@@ -67,7 +72,7 @@ func (suite *CacheTestSuite) Test_CacheDelete() {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
suite.T().Run(tt.name, func(t *testing.T) {
|
||||
cache.Set(tt.name, []byte(tt.name), 5*time.Second)
|
||||
cache.Set(tt.name, []byte(tt.name), 2*time.Second)
|
||||
c, ok := cache.Get(tt.name)
|
||||
suite.Equal(true, ok)
|
||||
suite.Equal(tt.name, string(c))
|
||||
@@ -80,7 +85,9 @@ func (suite *CacheTestSuite) Test_CacheDelete() {
|
||||
}
|
||||
|
||||
func (suite *CacheTestSuite) Test_CacheExpire() {
|
||||
cache := New(30 * time.Second)
|
||||
cache := New(5 * time.Second)
|
||||
defer cache.Stop()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cache_value string
|
||||
@@ -89,12 +96,12 @@ func (suite *CacheTestSuite) Test_CacheExpire() {
|
||||
{
|
||||
name: "test1",
|
||||
cache_value: "test1-123",
|
||||
ttl: 2 * time.Second,
|
||||
ttl: 100 * time.Millisecond,
|
||||
},
|
||||
{
|
||||
name: "test2",
|
||||
cache_value: "test2-123",
|
||||
ttl: 5 * time.Second,
|
||||
ttl: 200 * time.Millisecond,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
@@ -103,10 +110,210 @@ func (suite *CacheTestSuite) Test_CacheExpire() {
|
||||
c, ok := cache.Get(tt.name)
|
||||
suite.Equal(true, ok)
|
||||
suite.Equal(tt.name, string(c))
|
||||
time.Sleep(tt.ttl)
|
||||
time.Sleep(tt.ttl + 50*time.Millisecond) // Add small buffer
|
||||
c, ok = cache.Get(tt.name)
|
||||
suite.Equal(false, ok)
|
||||
suite.Equal("", string(c))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *CacheTestSuite) Test_CacheCompression() {
|
||||
cache := New(5 * time.Second)
|
||||
defer cache.Stop()
|
||||
|
||||
suite.T().Run("should compress large data", func(t *testing.T) {
|
||||
// Create data larger than compression threshold (1024 bytes)
|
||||
largeData := make([]byte, 2048)
|
||||
for i := range largeData {
|
||||
largeData[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
cache.Set("large_key", largeData, 2*time.Second)
|
||||
retrieved, ok := cache.Get("large_key")
|
||||
suite.True(ok)
|
||||
suite.Equal(largeData, retrieved)
|
||||
})
|
||||
|
||||
suite.T().Run("should handle compression errors gracefully", func(t *testing.T) {
|
||||
// Test with data that might cause compression issues
|
||||
largeData := make([]byte, 1500)
|
||||
for i := range largeData {
|
||||
largeData[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
cache.Set("compression_test", largeData, 2*time.Second)
|
||||
retrieved, ok := cache.Get("compression_test")
|
||||
suite.True(ok)
|
||||
suite.Equal(largeData, retrieved)
|
||||
})
|
||||
|
||||
suite.T().Run("should not compress small data", func(t *testing.T) {
|
||||
smallData := []byte("small data")
|
||||
cache.Set("small_key", smallData, 2*time.Second)
|
||||
retrieved, ok := cache.Get("small_key")
|
||||
suite.True(ok)
|
||||
suite.Equal(smallData, retrieved)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *CacheTestSuite) Test_CacheCompressionEdgeCases() {
|
||||
cache := New(5 * time.Second)
|
||||
defer cache.Stop()
|
||||
|
||||
suite.T().Run("should handle highly compressible data", func(t *testing.T) {
|
||||
// Create highly compressible data (repeated pattern)
|
||||
compressibleData := make([]byte, 2048)
|
||||
for i := range compressibleData {
|
||||
compressibleData[i] = 'A' // All same character
|
||||
}
|
||||
|
||||
cache.Set("compressible", compressibleData, 2*time.Second)
|
||||
retrieved, ok := cache.Get("compressible")
|
||||
suite.True(ok)
|
||||
suite.Equal(compressibleData, retrieved)
|
||||
})
|
||||
|
||||
suite.T().Run("should handle incompressible data", func(t *testing.T) {
|
||||
// Create random-like data that doesn't compress well
|
||||
incompressibleData := make([]byte, 1500)
|
||||
for i := range incompressibleData {
|
||||
incompressibleData[i] = byte((i*7 + i*i) % 256) // Pseudo-random pattern
|
||||
}
|
||||
|
||||
cache.Set("incompressible", incompressibleData, 2*time.Second)
|
||||
retrieved, ok := cache.Get("incompressible")
|
||||
suite.True(ok)
|
||||
suite.Equal(incompressibleData, retrieved)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *CacheTestSuite) Test_CacheCleanup() {
|
||||
cache := New(100 * time.Millisecond) // Short TTL for faster testing
|
||||
defer cache.Stop()
|
||||
|
||||
suite.T().Run("should clean expired entries", func(t *testing.T) {
|
||||
cache.Set("expire1", []byte("value1"), 50*time.Millisecond)
|
||||
cache.Set("expire2", []byte("value2"), 50*time.Millisecond)
|
||||
cache.Set("persist", []byte("persist"), 5*time.Second)
|
||||
|
||||
// Verify entries exist
|
||||
_, ok := cache.Get("expire1")
|
||||
suite.True(ok)
|
||||
_, ok = cache.Get("expire2")
|
||||
suite.True(ok)
|
||||
_, ok = cache.Get("persist")
|
||||
suite.True(ok)
|
||||
|
||||
// Wait for expiration
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Trigger cleanup
|
||||
cache.CleanExpiredEntries()
|
||||
|
||||
// Check expired entries are gone
|
||||
_, ok = cache.Get("expire1")
|
||||
suite.False(ok)
|
||||
_, ok = cache.Get("expire2")
|
||||
suite.False(ok)
|
||||
|
||||
// Check persistent entry still exists
|
||||
_, ok = cache.Get("persist")
|
||||
suite.True(ok)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *CacheTestSuite) Test_CacheLazyCleanup() {
|
||||
cache := New(5 * time.Second)
|
||||
defer cache.Stop()
|
||||
|
||||
suite.T().Run("should trigger lazy cleanup on expired access", func(t *testing.T) {
|
||||
cache.Set("lazy_expire", []byte("value"), 50*time.Millisecond)
|
||||
|
||||
// Verify entry exists
|
||||
_, ok := cache.Get("lazy_expire")
|
||||
suite.True(ok)
|
||||
|
||||
// Wait for expiration
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Access expired entry should trigger lazy cleanup
|
||||
_, ok = cache.Get("lazy_expire")
|
||||
suite.False(ok)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *CacheTestSuite) Test_CacheStop() {
|
||||
suite.T().Run("should stop cache gracefully", func(t *testing.T) {
|
||||
cache := New(5 * time.Second)
|
||||
|
||||
// Add some data
|
||||
cache.Set("test", []byte("value"), 2*time.Second)
|
||||
_, ok := cache.Get("test")
|
||||
suite.True(ok)
|
||||
|
||||
// Stop cache
|
||||
cache.Stop()
|
||||
|
||||
// Should be able to stop multiple times without panic
|
||||
cache.Stop()
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *CacheTestSuite) Test_CacheSharding() {
|
||||
cache := New(5 * time.Second)
|
||||
defer cache.Stop()
|
||||
|
||||
suite.T().Run("should distribute keys across shards", func(t *testing.T) {
|
||||
// Test multiple keys to ensure sharding works
|
||||
keys := []string{"key1", "key2", "key3", "key4", "key5"}
|
||||
|
||||
for _, key := range keys {
|
||||
cache.Set(key, []byte("value_"+key), 2*time.Second)
|
||||
}
|
||||
|
||||
for _, key := range keys {
|
||||
value, ok := cache.Get(key)
|
||||
suite.True(ok)
|
||||
suite.Equal("value_"+key, string(value))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *CacheTestSuite) Test_CachePeriodicCleanup() {
|
||||
suite.T().Run("should handle short TTL for periodic cleanup", func(t *testing.T) {
|
||||
// Test with very short TTL to trigger the minimum cleanup interval logic
|
||||
cache := New(500 * time.Millisecond)
|
||||
defer cache.Stop()
|
||||
|
||||
cache.Set("short_ttl", []byte("value"), 100*time.Millisecond)
|
||||
|
||||
// Wait for periodic cleanup to potentially run
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
_, ok := cache.Get("short_ttl")
|
||||
suite.False(ok)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *CacheTestSuite) Test_CacheDecompressionErrors() {
|
||||
cache := New(5 * time.Second)
|
||||
defer cache.Stop()
|
||||
|
||||
suite.T().Run("should handle decompression pool edge cases", func(t *testing.T) {
|
||||
// This test ensures the decompression pool handles nil readers correctly
|
||||
largeData := make([]byte, 2048)
|
||||
for i := range largeData {
|
||||
largeData[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
// Set and get multiple times to exercise the pool
|
||||
for i := 0; i < 5; i++ {
|
||||
key := "pool_test_" + string(rune('0'+i))
|
||||
cache.Set(key, largeData, 2*time.Second)
|
||||
retrieved, ok := cache.Get(key)
|
||||
suite.True(ok)
|
||||
suite.Equal(largeData, retrieved)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
cache "github.com/lukaszraczylo/go-simple-graphql/cache"
|
||||
)
|
||||
|
||||
// Benchmark cache compression with different data sizes
|
||||
func BenchmarkCacheCompression(b *testing.B) {
|
||||
c := cache.New(5 * time.Second) // Use shorter TTL for tests
|
||||
defer c.Stop()
|
||||
|
||||
// Small data (under 1KB threshold)
|
||||
smallData := make([]byte, 512)
|
||||
for i := range smallData {
|
||||
smallData[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
// Medium data (over 1KB threshold)
|
||||
mediumData := make([]byte, 2048)
|
||||
for i := range mediumData {
|
||||
mediumData[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
// Large data (much larger than threshold)
|
||||
largeData := make([]byte, 8192)
|
||||
for i := range largeData {
|
||||
largeData[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
// Highly compressible data
|
||||
compressibleData := make([]byte, 4096)
|
||||
for i := range compressibleData {
|
||||
compressibleData[i] = 'A' // Repeating character compresses well
|
||||
}
|
||||
|
||||
b.Run("SmallData_Set", func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
c.Set("small", smallData, time.Minute)
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("SmallData_Get", func(b *testing.B) {
|
||||
c.Set("small", smallData, time.Minute)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = c.Get("small")
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("MediumData_Set", func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
c.Set("medium", mediumData, time.Minute)
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("MediumData_Get", func(b *testing.B) {
|
||||
c.Set("medium", mediumData, time.Minute)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = c.Get("medium")
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("LargeData_Set", func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
c.Set("large", largeData, time.Minute)
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("LargeData_Get", func(b *testing.B) {
|
||||
c.Set("large", largeData, time.Minute)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = c.Get("large")
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("CompressibleData_Set", func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
c.Set("compressible", compressibleData, time.Minute)
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("CompressibleData_Get", func(b *testing.B) {
|
||||
c.Set("compressible", compressibleData, time.Minute)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = c.Get("compressible")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Benchmark cache memory usage with compression
|
||||
func BenchmarkCacheMemoryUsage(b *testing.B) {
|
||||
c := cache.New(5 * time.Second) // Use shorter TTL for tests
|
||||
defer c.Stop()
|
||||
|
||||
// Test data that compresses well
|
||||
testData := make([]byte, 4096)
|
||||
for i := range testData {
|
||||
testData[i] = byte(i%10 + '0') // Repeating pattern
|
||||
}
|
||||
|
||||
b.Run("WithCompression", func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
key := "key" + string(rune(i%1000))
|
||||
c.Set(key, testData, time.Minute)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Test compression effectiveness
|
||||
func TestCompressionEffectiveness(t *testing.T) {
|
||||
c := cache.New(5 * time.Second) // Use shorter TTL for tests
|
||||
defer c.Stop()
|
||||
|
||||
// Highly compressible data
|
||||
compressibleData := make([]byte, 4096)
|
||||
for i := range compressibleData {
|
||||
compressibleData[i] = 'A'
|
||||
}
|
||||
|
||||
// Random data (less compressible)
|
||||
randomData := make([]byte, 4096)
|
||||
for i := range randomData {
|
||||
randomData[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
// Small data (under threshold)
|
||||
smallData := make([]byte, 512)
|
||||
for i := range smallData {
|
||||
smallData[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
// Test compressible data
|
||||
c.Set("compressible", compressibleData, time.Minute)
|
||||
retrieved, ok := c.Get("compressible")
|
||||
if !ok {
|
||||
t.Error("Failed to retrieve compressible data")
|
||||
}
|
||||
if len(retrieved) != len(compressibleData) {
|
||||
t.Errorf("Data length mismatch: expected %d, got %d", len(compressibleData), len(retrieved))
|
||||
}
|
||||
|
||||
// Test random data
|
||||
c.Set("random", randomData, time.Minute)
|
||||
retrieved, ok = c.Get("random")
|
||||
if !ok {
|
||||
t.Error("Failed to retrieve random data")
|
||||
}
|
||||
if len(retrieved) != len(randomData) {
|
||||
t.Errorf("Data length mismatch: expected %d, got %d", len(randomData), len(retrieved))
|
||||
}
|
||||
|
||||
// Test small data (should not be compressed)
|
||||
c.Set("small", smallData, time.Minute)
|
||||
retrieved, ok = c.Get("small")
|
||||
if !ok {
|
||||
t.Error("Failed to retrieve small data")
|
||||
}
|
||||
if len(retrieved) != len(smallData) {
|
||||
t.Errorf("Data length mismatch: expected %d, got %d", len(smallData), len(retrieved))
|
||||
}
|
||||
}
|
||||
+130
-20
@@ -3,11 +3,11 @@ package gql
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/avast/retry-go/v4"
|
||||
@@ -15,11 +15,23 @@ import (
|
||||
libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging"
|
||||
)
|
||||
|
||||
var bufferPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return new(bytes.Buffer)
|
||||
},
|
||||
}
|
||||
var (
|
||||
// Shared HTTP transport with optimized settings
|
||||
defaultTransport = &http.Transport{
|
||||
MaxIdleConns: 100,
|
||||
MaxIdleConnsPerHost: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
DisableCompression: true, // Disable automatic compression to handle gzip manually
|
||||
ForceAttemptHTTP2: true,
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // Skip TLS verification for test environments
|
||||
}
|
||||
|
||||
// Shared HTTP client with timeouts
|
||||
defaultClient = &http.Client{
|
||||
Transport: defaultTransport,
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
)
|
||||
|
||||
func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
// Reuse buffer from pool to avoid allocations
|
||||
@@ -35,7 +47,7 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
Message: "Can't create HTTP request",
|
||||
Pairs: map[string]interface{}{"error": err.Error()},
|
||||
})
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("can't create HTTP request: %w", err)
|
||||
}
|
||||
|
||||
for key, value := range qe.Headers {
|
||||
@@ -46,6 +58,11 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
httpRequest.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
// Set Accept-Encoding header to request gzip compression
|
||||
if httpRequest.Header.Get("Accept-Encoding") == "" {
|
||||
httpRequest.Header.Set("Accept-Encoding", "gzip")
|
||||
}
|
||||
|
||||
retriesMax := 1
|
||||
if qe.Retries {
|
||||
retriesMax = qe.retries_number
|
||||
@@ -62,7 +79,12 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
httpRequest.Body = io.NopCloser(bytes.NewReader(buf.Bytes()))
|
||||
httpRequest.ContentLength = int64(buf.Len())
|
||||
|
||||
httpResponse, err := qe.client.Do(httpRequest)
|
||||
// Use default client if custom client is not set
|
||||
client := qe.client
|
||||
if client == nil {
|
||||
client = defaultClient
|
||||
}
|
||||
httpResponse, err := client.Do(httpRequest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -75,28 +97,115 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
return fmt.Errorf("HTTP error - status code: %s for %s", httpResponse.Status, httpRequest.URL)
|
||||
}
|
||||
|
||||
var reader io.Reader
|
||||
// Log Content-Encoding header for debugging
|
||||
encoding := httpResponse.Header.Get("Content-Encoding")
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Processing response",
|
||||
Pairs: map[string]interface{}{
|
||||
"content_encoding": encoding,
|
||||
"content_type": httpResponse.Header.Get("Content-Type"),
|
||||
},
|
||||
})
|
||||
|
||||
// First, read the entire response body into a buffer
|
||||
rawBuf := bufferPool.Get().(*bytes.Buffer)
|
||||
rawBuf.Reset()
|
||||
defer bufferPool.Put(rawBuf)
|
||||
|
||||
_, err = io.Copy(rawBuf, httpResponse.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading HTTP response body: %w", err)
|
||||
}
|
||||
|
||||
rawData := rawBuf.Bytes()
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Read raw response data",
|
||||
Pairs: map[string]interface{}{
|
||||
"response_size": len(rawData),
|
||||
},
|
||||
})
|
||||
|
||||
// Use buffer pool for final data processing
|
||||
respBuf := bufferPool.Get().(*bytes.Buffer)
|
||||
respBuf.Reset()
|
||||
defer bufferPool.Put(respBuf)
|
||||
|
||||
var finalData []byte
|
||||
|
||||
// Handle gzip decompression with fallback
|
||||
if encoding == "gzip" {
|
||||
gzipReader, err := gzip.NewReader(httpResponse.Body)
|
||||
// Try to create gzip reader from raw data
|
||||
gzipReader, err := gzip.NewReader(bytes.NewReader(rawData))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating gzip reader: %w", err)
|
||||
qe.Logger.Warning(&libpack_logger.LogMessage{
|
||||
Message: "Failed to create gzip reader, falling back to uncompressed parsing",
|
||||
Pairs: map[string]interface{}{"gzip_error": err.Error()},
|
||||
})
|
||||
// Fallback: treat raw data as uncompressed
|
||||
finalData = rawData
|
||||
} else {
|
||||
// Successfully created gzip reader, now try to decompress
|
||||
defer gzipReader.Close()
|
||||
|
||||
_, copyErr := io.Copy(respBuf, gzipReader)
|
||||
if copyErr != nil {
|
||||
// Gzip decompression failed, try fallback
|
||||
qe.Logger.Warning(&libpack_logger.LogMessage{
|
||||
Message: "Gzip decompression failed, falling back to uncompressed parsing",
|
||||
Pairs: map[string]interface{}{
|
||||
"copy_error": copyErr,
|
||||
"raw_size": len(rawData),
|
||||
},
|
||||
})
|
||||
// Fallback: treat raw data as uncompressed
|
||||
finalData = rawData
|
||||
} else {
|
||||
// Successful decompression
|
||||
finalData = respBuf.Bytes()
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Successfully decompressed gzip response",
|
||||
Pairs: map[string]interface{}{
|
||||
"compressed_size": len(rawData),
|
||||
"decompressed_size": len(finalData),
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
defer gzipReader.Close()
|
||||
reader = gzipReader
|
||||
} else {
|
||||
reader = httpResponse.Body
|
||||
// No compression, use raw data directly
|
||||
finalData = rawData
|
||||
}
|
||||
|
||||
// Read all response body
|
||||
body, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading HTTP response: %w", err)
|
||||
// Validate that we have data before attempting JSON unmarshaling
|
||||
if len(finalData) == 0 {
|
||||
return fmt.Errorf("empty response data after processing")
|
||||
}
|
||||
|
||||
// Unmarshal response
|
||||
err = json.Unmarshal(body, &queryResult)
|
||||
// Log the first few bytes for debugging (truncated for safety)
|
||||
debugData := finalData
|
||||
if len(debugData) > 100 {
|
||||
debugData = debugData[:100]
|
||||
}
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Attempting JSON unmarshaling",
|
||||
Pairs: map[string]interface{}{
|
||||
"data_size": len(finalData),
|
||||
"data_sample": string(debugData),
|
||||
},
|
||||
})
|
||||
|
||||
// Unmarshal the final processed data
|
||||
err = json.Unmarshal(finalData, &queryResult)
|
||||
if err != nil {
|
||||
qe.Logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "JSON unmarshaling failed",
|
||||
Pairs: map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"data_size": len(finalData),
|
||||
"data_sample": string(debugData),
|
||||
"encoding": encoding,
|
||||
},
|
||||
})
|
||||
return fmt.Errorf("error unmarshalling HTTP response: %w", err)
|
||||
}
|
||||
|
||||
@@ -111,6 +220,7 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
retry.Attempts(uint(retriesMax)),
|
||||
retry.DelayType(retry.BackOffDelay),
|
||||
retry.Delay(time.Duration(qe.retries_delay)),
|
||||
retry.MaxDelay(10*time.Second),
|
||||
retry.LastErrorOnly(true),
|
||||
)
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,594 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (suite *Tests) TestQueryExecutor_executeQuery() {
|
||||
suite.T().Run("should execute query successfully", func(t *testing.T) {
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "test-key",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = mockServer.URL
|
||||
qe.client = mockServer.Client()
|
||||
qe.cache = client.cache
|
||||
qe.CacheTTL = 5 * time.Second
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
})
|
||||
|
||||
suite.T().Run("should handle HTTP errors", func(t *testing.T) {
|
||||
// Create a test server that returns an error
|
||||
errorServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("Internal Server Error"))
|
||||
}))
|
||||
defer errorServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = errorServer.URL
|
||||
qe.client = errorServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
assert.Contains(err.Error(), "HTTP error")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle GraphQL errors", func(t *testing.T) {
|
||||
// Create a test server that returns GraphQL errors
|
||||
errorServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"errors":[{"message":"Field not found"}]}`))
|
||||
}))
|
||||
defer errorServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { invalidField }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = errorServer.URL
|
||||
qe.client = errorServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
assert.Contains(err.Error(), "error executing query")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle no data response", func(t *testing.T) {
|
||||
// Create a test server that returns no data
|
||||
noDataServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"data":null}`))
|
||||
}))
|
||||
defer noDataServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = noDataServer.URL
|
||||
qe.client = noDataServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
assert.Contains(err.Error(), "no data")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle gzip compressed responses", func(t *testing.T) {
|
||||
// Create a test server that returns gzip compressed data
|
||||
gzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
var buf bytes.Buffer
|
||||
gzipWriter := gzip.NewWriter(&buf)
|
||||
gzipWriter.Write([]byte(`{"data":{"viewer":{"login":"testuser"}}}`))
|
||||
gzipWriter.Close()
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(buf.Bytes())
|
||||
}))
|
||||
defer gzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "test-gzip",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = gzipServer.URL
|
||||
qe.client = gzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
qe.CacheTTL = 5 * time.Second
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
assert.Contains(string(result), "testuser")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle retries", func(t *testing.T) {
|
||||
attempts := 0
|
||||
retryServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
attempts++
|
||||
if attempts < 3 {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"data":{"viewer":{"login":"retryuser"}}}`))
|
||||
}))
|
||||
defer retryServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: true,
|
||||
}
|
||||
qe.endpoint = retryServer.URL
|
||||
qe.client = retryServer.Client()
|
||||
qe.cache = client.cache
|
||||
qe.retries_number = 5
|
||||
qe.retries_delay = 100 * time.Millisecond
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
assert.Contains(string(result), "retryuser")
|
||||
assert.Equal(3, attempts) // Should have retried twice before succeeding
|
||||
})
|
||||
|
||||
suite.T().Run("should use default client when none provided", func(t *testing.T) {
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = mockServer.URL
|
||||
qe.client = nil // No client provided
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
})
|
||||
|
||||
suite.T().Run("should set default Content-Type header", func(t *testing.T) {
|
||||
var receivedHeaders http.Header
|
||||
headerServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
receivedHeaders = r.Header
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"data":{"viewer":{"login":"headeruser"}}}`))
|
||||
}))
|
||||
defer headerServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{}, // No Content-Type header
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = headerServer.URL
|
||||
qe.client = headerServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
assert.Equal("application/json", receivedHeaders.Get("Content-Type"))
|
||||
})
|
||||
|
||||
suite.T().Run("should handle invalid JSON response", func(t *testing.T) {
|
||||
invalidJSONServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`invalid json response`))
|
||||
}))
|
||||
defer invalidJSONServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = invalidJSONServer.URL
|
||||
qe.client = invalidJSONServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
assert.Contains(err.Error(), "error unmarshalling HTTP response")
|
||||
})
|
||||
|
||||
suite.T().Run("should cache successful responses", func(t *testing.T) {
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "cache-test-key",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = mockServer.URL
|
||||
qe.client = mockServer.Client()
|
||||
qe.cache = client.cache
|
||||
qe.CacheTTL = 5 * time.Second
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
|
||||
// Check that the result was cached
|
||||
cachedResult := client.cacheLookup("cache-test-key")
|
||||
assert.NotNil(cachedResult)
|
||||
assert.Equal(result, cachedResult)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestQueryExecutor_executeQuery_requestCreationError() {
|
||||
suite.T().Run("should handle request creation errors", func(t *testing.T) {
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = "://invalid-url" // Invalid URL to trigger error
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
assert.Contains(err.Error(), "can't create HTTP request")
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestQueryExecutor_executeQuery_gzipTrailingGarbage() {
|
||||
suite.T().Run("should handle gzip trailing garbage error with fallback", func(t *testing.T) {
|
||||
// Create a test server that returns malformed gzip data (with trailing garbage)
|
||||
malformedGzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
// Create valid gzip data first
|
||||
var buf bytes.Buffer
|
||||
gzipWriter := gzip.NewWriter(&buf)
|
||||
gzipWriter.Write([]byte(`{"data":{"viewer":{"login":"testuser"}}}`))
|
||||
gzipWriter.Close()
|
||||
|
||||
// Add trailing garbage to simulate the error
|
||||
validGzipData := buf.Bytes()
|
||||
malformedData := append(validGzipData, []byte("trailing garbage data")...)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(malformedData)
|
||||
}))
|
||||
defer malformedGzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = malformedGzipServer.URL
|
||||
qe.client = malformedGzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
// This should fail with gzip decompression but fallback to treating as uncompressed
|
||||
result, err := qe.executeQuery()
|
||||
// The fallback should fail because the raw data (including gzip headers) is not valid JSON
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
assert.Contains(err.Error(), "error unmarshalling HTTP response")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle gzip with valid JSON fallback", func(t *testing.T) {
|
||||
// Create a test server that claims gzip but returns plain JSON
|
||||
fakeGzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip") // Claims gzip but sends plain JSON
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"data":{"viewer":{"login":"fallbackuser"}}}`))
|
||||
}))
|
||||
defer fakeGzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = fakeGzipServer.URL
|
||||
qe.client = fakeGzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
// Should fail gzip decompression but succeed with fallback to plain JSON
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
assert.Contains(string(result), "fallbackuser")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle corrupted gzip header", func(t *testing.T) {
|
||||
// Create a test server that returns corrupted gzip data
|
||||
corruptedGzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
// Send corrupted gzip data (invalid magic number)
|
||||
corruptedData := []byte{0x1f, 0x8c, 0x08, 0x00} // Invalid gzip header
|
||||
corruptedData = append(corruptedData, []byte(`{"data":{"viewer":{"login":"corruptuser"}}}`)...)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(corruptedData)
|
||||
}))
|
||||
defer corruptedGzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = corruptedGzipServer.URL
|
||||
qe.client = corruptedGzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
// Should fail gzip reader creation and fallback to raw data parsing
|
||||
result, err := qe.executeQuery()
|
||||
// This will likely fail because the raw data includes corrupted gzip headers
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
})
|
||||
|
||||
suite.T().Run("should handle empty gzip response", func(t *testing.T) {
|
||||
// Create a test server that returns empty gzip data
|
||||
emptyGzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
// Send empty response
|
||||
}))
|
||||
defer emptyGzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = emptyGzipServer.URL
|
||||
qe.client = emptyGzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
assert.Contains(err.Error(), "empty response data")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle partial gzip data", func(t *testing.T) {
|
||||
// Create a test server that returns partial/truncated gzip data
|
||||
partialGzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
// Create valid gzip data but truncate it
|
||||
var buf bytes.Buffer
|
||||
gzipWriter := gzip.NewWriter(&buf)
|
||||
gzipWriter.Write([]byte(`{"data":{"viewer":{"login":"partialuser"}}}`))
|
||||
gzipWriter.Close()
|
||||
|
||||
// Send only partial gzip data (truncated)
|
||||
fullData := buf.Bytes()
|
||||
partialData := fullData[:len(fullData)/2] // Send only half
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(partialData)
|
||||
}))
|
||||
defer partialGzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = partialGzipServer.URL
|
||||
qe.client = partialGzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
// Should fail gzip decompression and fallback, but raw data won't be valid JSON
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
})
|
||||
|
||||
suite.T().Run("should successfully decompress valid gzip", func(t *testing.T) {
|
||||
// Ensure our fix doesn't break valid gzip handling
|
||||
validGzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
var buf bytes.Buffer
|
||||
gzipWriter := gzip.NewWriter(&buf)
|
||||
gzipWriter.Write([]byte(`{"data":{"viewer":{"login":"validgzipuser"}}}`))
|
||||
gzipWriter.Close()
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(buf.Bytes())
|
||||
}))
|
||||
defer validGzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "test-valid-gzip",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = validGzipServer.URL
|
||||
qe.client = validGzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
qe.CacheTTL = 5 * time.Second
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
assert.Contains(string(result), "validgzipuser")
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestQueryExecutor_executeQuery_gzipErrorScenarios() {
|
||||
suite.T().Run("should handle gzip reader creation failure", func(t *testing.T) {
|
||||
// Test server that sends invalid gzip magic number
|
||||
invalidMagicServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
// Send data with wrong magic number (not gzip)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"data":{"viewer":{"login":"magicuser"}}}`))
|
||||
}))
|
||||
defer invalidMagicServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = invalidMagicServer.URL
|
||||
qe.client = invalidMagicServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
// Should fail gzip reader creation, fallback to raw JSON parsing, and succeed
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
assert.Contains(string(result), "magicuser")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle gzip decompression with unexpected EOF", func(t *testing.T) {
|
||||
// Create server that sends truncated gzip stream
|
||||
eofGzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
// Create a proper gzip header but truncate the data
|
||||
gzipHeader := []byte{0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}
|
||||
// Add some compressed data but not complete
|
||||
incompleteData := []byte{0x4a, 0x49, 0x2c, 0x49, 0x54, 0xb2, 0x52, 0x50}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(append(gzipHeader, incompleteData...))
|
||||
}))
|
||||
defer eofGzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = eofGzipServer.URL
|
||||
qe.client = eofGzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
// Should fail gzip decompression and fallback, but raw data won't be valid JSON
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
assert.Contains(err.Error(), "error unmarshalling HTTP response")
|
||||
})
|
||||
}
|
||||
@@ -1,14 +1,16 @@
|
||||
module github.com/lukaszraczylo/go-simple-graphql
|
||||
|
||||
go 1.21
|
||||
go 1.23.0
|
||||
|
||||
toolchain go1.23.6
|
||||
|
||||
require (
|
||||
github.com/avast/retry-go/v4 v4.6.0
|
||||
github.com/goccy/go-json v0.10.3
|
||||
github.com/avast/retry-go/v4 v4.6.1
|
||||
github.com/goccy/go-json v0.10.5
|
||||
github.com/goccy/go-reflect v1.2.0
|
||||
github.com/gookit/goutil v0.6.16
|
||||
github.com/stretchr/testify v1.9.0
|
||||
golang.org/x/net v0.29.0
|
||||
github.com/gookit/goutil v0.6.18
|
||||
github.com/stretchr/testify v1.10.0
|
||||
golang.org/x/net v0.40.0
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -16,9 +18,9 @@ require (
|
||||
github.com/gookit/color v1.5.4 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.25.0 // indirect
|
||||
golang.org/x/term v0.24.0 // indirect
|
||||
golang.org/x/text v0.18.0 // indirect
|
||||
golang.org/x/sync v0.14.0 // indirect
|
||||
golang.org/x/sys v0.33.0 // indirect
|
||||
golang.org/x/term v0.32.0 // indirect
|
||||
golang.org/x/text v0.25.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
github.com/avast/retry-go/v4 v4.6.0 h1:K9xNA+KeB8HHc2aWFuLb25Offp+0iVRXEvFx8IinRJA=
|
||||
github.com/avast/retry-go/v4 v4.6.0/go.mod h1:gvWlPhBVsvBbLkVGDg/KwvBv0bEkCOLRRSHKIr2PyOE=
|
||||
github.com/avast/retry-go/v4 v4.6.1 h1:VkOLRubHdisGrHnTu89g08aQEWEgRU7LVEop3GbIcMk=
|
||||
github.com/avast/retry-go/v4 v4.6.1/go.mod h1:V6oF8njAwxJ5gRo1Q7Cxab24xs5NCWZBeaHHBklR8mA=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
|
||||
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/goccy/go-reflect v1.2.0 h1:O0T8rZCuNmGXewnATuKYnkL0xm6o8UNOJZd/gOkb9ms=
|
||||
github.com/goccy/go-reflect v1.2.0/go.mod h1:n0oYZn8VcV2CkWTxi8B9QjkCoq6GTtCEdfmR66YhFtE=
|
||||
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
|
||||
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
|
||||
github.com/gookit/goutil v0.6.16 h1:9fRMCF4X9abdRD5+2HhBS/GwafjBlTUBjRtA5dgkvuw=
|
||||
github.com/gookit/goutil v0.6.16/go.mod h1:op2q8AoPDFSiY2+qkHxcBWQMYxOLQ1GbLXqe7vrwscI=
|
||||
github.com/gookit/goutil v0.6.18 h1:MUVj0G16flubWT8zYVicIuisUiHdgirPAkmnfD2kKgw=
|
||||
github.com/gookit/goutil v0.6.18/go.mod h1:AY/5sAwKe7Xck+mEbuxj0n/bc3qwrGNe3Oeulln7zBA=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
|
||||
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
|
||||
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
|
||||
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
||||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
|
||||
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
|
||||
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
|
||||
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
|
||||
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
|
||||
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
|
||||
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
|
||||
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
|
||||
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
|
||||
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Benchmark_calculateHash(b *testing.B) {
|
||||
client := NewConnection()
|
||||
query := &Query{
|
||||
Query: "query { viewer { login } }",
|
||||
Variables: map[string]interface{}{"var1": "value1"},
|
||||
}
|
||||
query.JsonQuery = client.convertToJSON(query)
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = calculateHash(query)
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_calculateHashLargeQuery(b *testing.B) {
|
||||
client := NewConnection()
|
||||
// Create a larger query to test hash performance with bigger payloads
|
||||
largeQuery := `query {
|
||||
viewer {
|
||||
login
|
||||
name
|
||||
email
|
||||
bio
|
||||
company
|
||||
location
|
||||
websiteUrl
|
||||
twitterUsername
|
||||
repositories(first: 100) {
|
||||
nodes {
|
||||
name
|
||||
description
|
||||
url
|
||||
stargazerCount
|
||||
forkCount
|
||||
primaryLanguage {
|
||||
name
|
||||
color
|
||||
}
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
variables := make(map[string]interface{})
|
||||
for i := 0; i < 50; i++ {
|
||||
variables[fmt.Sprintf("var%d", i)] = fmt.Sprintf("value%d", i)
|
||||
}
|
||||
|
||||
query := &Query{
|
||||
Query: largeQuery,
|
||||
Variables: variables,
|
||||
}
|
||||
query.JsonQuery = client.convertToJSON(query)
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = calculateHash(query)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"hash/fnv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Benchmark comparing MD5 vs FNV hash performance
|
||||
func BenchmarkHashAlgorithms(b *testing.B) {
|
||||
testData := []byte(`{"query":"query GetUser($id: ID!) { user(id: $id) { id name email profile { avatar bio } posts { id title content createdAt } } }","variables":{"id":"12345"}}`)
|
||||
|
||||
b.Run("MD5", func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
hash := md5.Sum(testData)
|
||||
_ = hex.EncodeToString(hash[:])
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("FNV64a", func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
hash := fnv.New64a()
|
||||
hash.Write(testData)
|
||||
_ = hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Benchmark the new calculateHash function
|
||||
func BenchmarkCalculateHash(b *testing.B) {
|
||||
query := &Query{
|
||||
Query: "query GetUser($id: ID!) { user(id: $id) { id name email profile { avatar bio } posts { id title content createdAt } } }",
|
||||
Variables: map[string]interface{}{
|
||||
"id": "12345",
|
||||
},
|
||||
JsonQuery: []byte(`{"query":"query GetUser($id: ID!) { user(id: $id) { id name email profile { avatar bio } posts { id title content createdAt } } }","variables":{"id":"12345"}}`),
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = calculateHash(query)
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark hash collision rate (quality test)
|
||||
func BenchmarkHashCollisions(b *testing.B) {
|
||||
queries := make([]*Query, 1000)
|
||||
for i := 0; i < 1000; i++ {
|
||||
queries[i] = &Query{
|
||||
Query: "query GetUser($id: ID!) { user(id: $id) { id name email } }",
|
||||
Variables: map[string]interface{}{
|
||||
"id": i,
|
||||
},
|
||||
JsonQuery: []byte(`{"query":"query GetUser($id: ID!) { user(id: $id) { id name email } }","variables":{"id":` + string(rune(i)) + `}}`),
|
||||
}
|
||||
}
|
||||
|
||||
b.Run("FNV_Collision_Test", func(b *testing.B) {
|
||||
hashes := make(map[string]bool)
|
||||
collisions := 0
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, query := range queries {
|
||||
hash := calculateHash(query)
|
||||
if hashes[hash] {
|
||||
collisions++
|
||||
} else {
|
||||
hashes[hash] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
b.ReportMetric(float64(collisions), "collisions")
|
||||
})
|
||||
}
|
||||
+5
-4
@@ -1,9 +1,9 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging"
|
||||
@@ -17,8 +17,9 @@ func searchForKeysInMapStringInterface(msi map[string]interface{}, key string) i
|
||||
}
|
||||
|
||||
func calculateHash(query *Query) string {
|
||||
hash := md5.Sum(query.JsonQuery)
|
||||
return hex.EncodeToString(hash[:])
|
||||
hash := fnv.New64a()
|
||||
hash.Write(query.JsonQuery)
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
|
||||
func (b *BaseClient) cacheLookup(hash string) []byte {
|
||||
@@ -48,6 +49,6 @@ func (b *BaseClient) decodeResponse(response []byte) (any, error) {
|
||||
Message: "Can't decode response",
|
||||
Pairs: map[string]interface{}{"error": "unknown response type"},
|
||||
})
|
||||
return nil, fmt.Errorf("Can't decode response - unknown response type specified")
|
||||
return nil, fmt.Errorf("can't decode response - unknown response type specified")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package gql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/goccy/go-reflect"
|
||||
)
|
||||
@@ -105,3 +106,64 @@ func (suite *Tests) TestBaseClient_decodeResponse() {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *Tests) Test_searchForKeysInMapStringInterface_nil() {
|
||||
suite.T().Run("should handle nil map", func(t *testing.T) {
|
||||
result := searchForKeysInMapStringInterface(nil, "test")
|
||||
assert.Nil(result)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) Test_calculateHash() {
|
||||
suite.T().Run("should calculate consistent hash", func(t *testing.T) {
|
||||
query1 := &Query{JsonQuery: []byte(`{"query": "{ user { name } }"}`)}
|
||||
query2 := &Query{JsonQuery: []byte(`{"query": "{ user { name } }"}`)}
|
||||
query3 := &Query{JsonQuery: []byte(`{"query": "{ user { email } }"}`)}
|
||||
|
||||
hash1 := calculateHash(query1)
|
||||
hash2 := calculateHash(query2)
|
||||
hash3 := calculateHash(query3)
|
||||
|
||||
// Same queries should produce same hash
|
||||
assert.Equal(hash1, hash2)
|
||||
// Different queries should produce different hashes
|
||||
assert.NotEqual(hash1, hash3)
|
||||
// Hash should be non-empty
|
||||
assert.NotEmpty(hash1)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestBaseClient_cacheLookup() {
|
||||
suite.T().Run("should lookup cache entries", func(t *testing.T) {
|
||||
client := CreateTestClient()
|
||||
|
||||
// Test cache miss
|
||||
result := client.cacheLookup("nonexistent")
|
||||
assert.Nil(result)
|
||||
|
||||
// Test cache hit
|
||||
testData := []byte("test data")
|
||||
client.cache.Set("test_key", testData, 5*time.Second)
|
||||
result = client.cacheLookup("test_key")
|
||||
assert.Equal(testData, result)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestBaseClient_decodeResponse_errors() {
|
||||
suite.T().Run("should handle invalid JSON for mapstring", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetOutput("mapstring")
|
||||
|
||||
_, err := client.decodeResponse([]byte(`invalid json`))
|
||||
assert.Error(err)
|
||||
})
|
||||
|
||||
suite.T().Run("should handle unknown response type", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.responseType = "unknown"
|
||||
|
||||
_, err := client.decodeResponse([]byte(`{"test": "data"}`))
|
||||
assert.Error(err)
|
||||
assert.Contains(err.Error(), "unknown response type")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,19 +11,22 @@ import (
|
||||
)
|
||||
|
||||
func (b *BaseClient) createHttpClient() (http_client *http.Client) {
|
||||
// Optimized connection pool settings for better performance
|
||||
httpTransport := &http.Transport{
|
||||
MaxIdleConns: 512,
|
||||
MaxConnsPerHost: 512,
|
||||
MaxIdleConnsPerHost: 512,
|
||||
IdleConnTimeout: 15 * time.Second,
|
||||
ResponseHeaderTimeout: 15 * time.Second,
|
||||
MaxIdleConns: 100, // Reduced from 512
|
||||
MaxConnsPerHost: 50, // Reduced from 512
|
||||
MaxIdleConnsPerHost: 10, // Reduced from 512
|
||||
IdleConnTimeout: 30 * time.Second, // Increased for better reuse
|
||||
ResponseHeaderTimeout: 10 * time.Second, // Reduced for faster timeouts
|
||||
DisableKeepAlives: false,
|
||||
DisableCompression: false,
|
||||
WriteBufferSize: 4096, // Optimize buffer size
|
||||
ReadBufferSize: 4096, // Optimize buffer size
|
||||
}
|
||||
|
||||
if strings.HasPrefix(b.endpoint, "http://") {
|
||||
http_client = &http.Client{
|
||||
Timeout: 15 * time.Second,
|
||||
Timeout: 30 * time.Second, // Increased for better reliability
|
||||
Transport: httpTransport,
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
@@ -39,12 +42,14 @@ func (b *BaseClient) createHttpClient() (http_client *http.Client) {
|
||||
tlsClientConfig.InsecureSkipVerify = true
|
||||
}
|
||||
http2Transport := &http2.Transport{
|
||||
AllowHTTP: true,
|
||||
TLSClientConfig: tlsClientConfig,
|
||||
ReadIdleTimeout: 15 * time.Second,
|
||||
PingTimeout: 15 * time.Second,
|
||||
AllowHTTP: true,
|
||||
TLSClientConfig: tlsClientConfig,
|
||||
ReadIdleTimeout: 30 * time.Second, // Increased for better reuse
|
||||
PingTimeout: 10 * time.Second, // Reduced for faster detection
|
||||
WriteByteTimeout: 10 * time.Second, // Add write timeout
|
||||
}
|
||||
http_client = &http.Client{
|
||||
Timeout: 30 * time.Second, // Increased for better reliability
|
||||
Transport: http2Transport,
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
@@ -58,6 +63,7 @@ func (b *BaseClient) createHttpClient() (http_client *http.Client) {
|
||||
b.Logger.Critical(&libpack_logger.LogMessage{
|
||||
Message: "Invalid endpoint - neither http or https",
|
||||
})
|
||||
return nil
|
||||
}
|
||||
return http_client
|
||||
}
|
||||
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/http2"
|
||||
)
|
||||
|
||||
func (suite *Tests) TestBaseClient_createHttpClient() {
|
||||
suite.T().Run("should create HTTP/1.1 client for http endpoints", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("http://example.com/graphql")
|
||||
|
||||
httpClient := client.createHttpClient()
|
||||
assert.NotNil(httpClient)
|
||||
assert.Equal(30*time.Second, httpClient.Timeout)
|
||||
assert.NotNil(httpClient.Transport)
|
||||
|
||||
// Check that it's using regular HTTP transport
|
||||
transport, ok := httpClient.Transport.(*http.Transport)
|
||||
assert.True(ok)
|
||||
assert.Equal(100, transport.MaxIdleConns)
|
||||
assert.Equal(50, transport.MaxConnsPerHost)
|
||||
assert.Equal(10, transport.MaxIdleConnsPerHost)
|
||||
assert.Equal(30*time.Second, transport.IdleConnTimeout)
|
||||
assert.Equal(10*time.Second, transport.ResponseHeaderTimeout)
|
||||
assert.False(transport.DisableKeepAlives)
|
||||
assert.False(transport.DisableCompression)
|
||||
assert.Equal(4096, transport.WriteBufferSize)
|
||||
assert.Equal(4096, transport.ReadBufferSize)
|
||||
})
|
||||
|
||||
suite.T().Run("should create HTTP/2 client for https endpoints", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("https://example.com/graphql")
|
||||
|
||||
httpClient := client.createHttpClient()
|
||||
assert.NotNil(httpClient)
|
||||
assert.Equal(30*time.Second, httpClient.Timeout)
|
||||
assert.NotNil(httpClient.Transport)
|
||||
|
||||
// Check that it's using HTTP/2 transport
|
||||
transport, ok := httpClient.Transport.(*http2.Transport)
|
||||
assert.True(ok)
|
||||
assert.True(transport.AllowHTTP)
|
||||
assert.NotNil(transport.TLSClientConfig)
|
||||
assert.True(transport.TLSClientConfig.InsecureSkipVerify)
|
||||
assert.Equal(30*time.Second, transport.ReadIdleTimeout)
|
||||
assert.Equal(10*time.Second, transport.PingTimeout)
|
||||
assert.Equal(10*time.Second, transport.WriteByteTimeout)
|
||||
})
|
||||
|
||||
suite.T().Run("should handle invalid endpoints", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("ftp://example.com/graphql")
|
||||
|
||||
httpClient := client.createHttpClient()
|
||||
assert.Nil(httpClient)
|
||||
})
|
||||
|
||||
suite.T().Run("should set redirect policy", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("http://example.com/graphql")
|
||||
|
||||
httpClient := client.createHttpClient()
|
||||
assert.NotNil(httpClient)
|
||||
assert.NotNil(httpClient.CheckRedirect)
|
||||
|
||||
// Test redirect policy
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
err := httpClient.CheckRedirect(req, []*http.Request{})
|
||||
assert.Equal(http.ErrUseLastResponse, err)
|
||||
})
|
||||
|
||||
suite.T().Run("should handle empty endpoint", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("")
|
||||
|
||||
httpClient := client.createHttpClient()
|
||||
assert.Nil(httpClient)
|
||||
})
|
||||
|
||||
suite.T().Run("should handle endpoint without protocol", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("example.com/graphql")
|
||||
|
||||
httpClient := client.createHttpClient()
|
||||
assert.Nil(httpClient)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestBaseClient_createHttpClient_transportSettings() {
|
||||
suite.T().Run("should configure HTTP transport correctly", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("http://example.com/graphql")
|
||||
|
||||
httpClient := client.createHttpClient()
|
||||
transport := httpClient.Transport.(*http.Transport)
|
||||
|
||||
// Verify all transport settings
|
||||
assert.Equal(100, transport.MaxIdleConns)
|
||||
assert.Equal(50, transport.MaxConnsPerHost)
|
||||
assert.Equal(10, transport.MaxIdleConnsPerHost)
|
||||
assert.Equal(30*time.Second, transport.IdleConnTimeout)
|
||||
assert.Equal(10*time.Second, transport.ResponseHeaderTimeout)
|
||||
assert.False(transport.DisableKeepAlives)
|
||||
assert.False(transport.DisableCompression)
|
||||
assert.Equal(4096, transport.WriteBufferSize)
|
||||
assert.Equal(4096, transport.ReadBufferSize)
|
||||
})
|
||||
|
||||
suite.T().Run("should configure HTTP/2 transport correctly", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("https://example.com/graphql")
|
||||
|
||||
httpClient := client.createHttpClient()
|
||||
transport := httpClient.Transport.(*http2.Transport)
|
||||
|
||||
// Verify all HTTP/2 transport settings
|
||||
assert.True(transport.AllowHTTP)
|
||||
assert.NotNil(transport.TLSClientConfig)
|
||||
assert.True(transport.TLSClientConfig.InsecureSkipVerify)
|
||||
assert.Equal(30*time.Second, transport.ReadIdleTimeout)
|
||||
assert.Equal(10*time.Second, transport.PingTimeout)
|
||||
assert.Equal(10*time.Second, transport.WriteByteTimeout)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestBaseClient_createHttpClient_logging() {
|
||||
suite.T().Run("should log HTTP/1.1 usage", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("http://example.com/graphql")
|
||||
|
||||
// This test verifies that the function runs without error
|
||||
// The actual logging is tested indirectly through the logger tests
|
||||
httpClient := client.createHttpClient()
|
||||
assert.NotNil(httpClient)
|
||||
})
|
||||
|
||||
suite.T().Run("should log HTTP/2 usage", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("https://example.com/graphql")
|
||||
|
||||
// This test verifies that the function runs without error
|
||||
// The actual logging is tested indirectly through the logger tests
|
||||
httpClient := client.createHttpClient()
|
||||
assert.NotNil(httpClient)
|
||||
})
|
||||
|
||||
suite.T().Run("should log critical error for invalid endpoint", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("invalid://example.com/graphql")
|
||||
|
||||
// This test verifies that the function runs without error
|
||||
// and returns nil for invalid endpoints (Critical() won't exit in tests)
|
||||
httpClient := client.createHttpClient()
|
||||
assert.Nil(httpClient)
|
||||
})
|
||||
}
|
||||
+5
-2
@@ -112,7 +112,7 @@ func (l *Logger) log(level int, m *LogMessage) {
|
||||
defer bufferPool.Put(buffer)
|
||||
buffer.Reset()
|
||||
|
||||
var encoder = json.NewEncoder(buffer)
|
||||
encoder := json.NewEncoder(buffer)
|
||||
err := encoder.Encode(m.Pairs)
|
||||
if err != nil {
|
||||
fmt.Println("Error marshalling log message:", err)
|
||||
@@ -167,7 +167,10 @@ func (l *Logger) Fatal(m *LogMessage) {
|
||||
|
||||
func (l *Logger) Critical(m *LogMessage) {
|
||||
l.Fatal(m)
|
||||
os.Exit(1)
|
||||
// Only call os.Exit(1) if not running in tests
|
||||
if flag.Lookup("test.v") == nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) shouldLog(level int) bool {
|
||||
|
||||
@@ -58,11 +58,11 @@ func Benchmark_NewLogger(b *testing.B) {
|
||||
got := New()
|
||||
|
||||
if tt.triggers.ModFormat.Format != "" {
|
||||
got = got.SetFormat(tt.triggers.ModFormat.Format)
|
||||
got.SetFormat(tt.triggers.ModFormat.Format)
|
||||
}
|
||||
|
||||
if tt.triggers.ModLevel.Level != 0 {
|
||||
got = got.SetMinLogLevel(tt.triggers.ModLevel.Level)
|
||||
got.SetMinLogLevel(tt.triggers.ModLevel.Level)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
+242
-25
@@ -3,7 +3,6 @@ package libpack_logger
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -11,30 +10,6 @@ import (
|
||||
"github.com/goccy/go-reflect"
|
||||
)
|
||||
|
||||
func captureStderr(f func()) string {
|
||||
originalStderr := os.Stderr
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stderr = w
|
||||
f()
|
||||
w.Close()
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
os.Stderr = originalStderr
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func captureStdOut(f func()) string {
|
||||
originalStdout := os.Stdout
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdout = w
|
||||
f()
|
||||
w.Close()
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
os.Stdout = originalStdout
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func (suite *LoggerTestSuite) Test_LogMessageString() {
|
||||
msg := &LogMessage{
|
||||
Message: "test message",
|
||||
@@ -180,3 +155,245 @@ func (suite *LoggerTestSuite) Test_ShouldLog() {
|
||||
assert.False(logger.shouldLog(LEVEL_INFO))
|
||||
assert.False(logger.shouldLog(LEVEL_DEBUG))
|
||||
}
|
||||
|
||||
func (suite *LoggerTestSuite) Test_GetLogLevel() {
|
||||
tests := []struct {
|
||||
name string
|
||||
level string
|
||||
expected int
|
||||
}{
|
||||
{"debug level", "debug", LEVEL_DEBUG},
|
||||
{"info level", "info", LEVEL_INFO},
|
||||
{"warn level", "warn", LEVEL_WARN},
|
||||
{"error level", "error", LEVEL_ERROR},
|
||||
{"fatal level", "fatal", LEVEL_FATAL},
|
||||
{"uppercase debug", "DEBUG", LEVEL_DEBUG},
|
||||
{"mixed case warn", "WaRn", LEVEL_WARN},
|
||||
{"unknown level", "unknown", defaultMinLevel},
|
||||
{"empty string", "", defaultMinLevel},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
suite.T().Run(tt.name, func(t *testing.T) {
|
||||
result := GetLogLevel(tt.level)
|
||||
assert.Equal(tt.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *LoggerTestSuite) Test_Warning() {
|
||||
output := &bytes.Buffer{}
|
||||
logger := New().SetOutput(output).SetMinLogLevel(LEVEL_WARN)
|
||||
|
||||
msg := &LogMessage{
|
||||
Message: "warning message",
|
||||
Pairs: map[string]any{"key": "value"},
|
||||
}
|
||||
|
||||
logger.Warning(msg)
|
||||
|
||||
logOutput := output.String()
|
||||
fmt.Printf("DEBUG: logOutput = %q\n", logOutput)
|
||||
fmt.Printf("DEBUG: logOutput length = %d\n", len(logOutput))
|
||||
assert.NotEmpty(logOutput)
|
||||
|
||||
var loggedMessage map[string]any
|
||||
err := json.Unmarshal([]byte(logOutput), &loggedMessage)
|
||||
fmt.Printf("DEBUG: unmarshal error = %v\n", err)
|
||||
fmt.Printf("DEBUG: loggedMessage = %+v\n", loggedMessage)
|
||||
assert.NoError(err)
|
||||
assert.Equal("warn", loggedMessage["level"])
|
||||
assert.Equal("warning message", loggedMessage["message"])
|
||||
}
|
||||
|
||||
func (suite *LoggerTestSuite) Test_Error() {
|
||||
output := &bytes.Buffer{}
|
||||
logger := New().SetOutput(output).SetMinLogLevel(LEVEL_ERROR)
|
||||
|
||||
msg := &LogMessage{
|
||||
Message: "error message",
|
||||
Pairs: map[string]any{"error": "test error"},
|
||||
}
|
||||
|
||||
logger.Error(msg)
|
||||
|
||||
logOutput := output.String()
|
||||
assert.NotEmpty(logOutput)
|
||||
|
||||
var loggedMessage map[string]any
|
||||
err := json.Unmarshal([]byte(logOutput), &loggedMessage)
|
||||
assert.NoError(err)
|
||||
assert.Equal("error", loggedMessage["level"])
|
||||
assert.Equal("error message", loggedMessage["message"])
|
||||
assert.Equal("test error", loggedMessage["error"])
|
||||
}
|
||||
|
||||
func (suite *LoggerTestSuite) Test_Fatal() {
|
||||
output := &bytes.Buffer{}
|
||||
logger := New().SetOutput(output).SetMinLogLevel(LEVEL_FATAL)
|
||||
|
||||
msg := &LogMessage{
|
||||
Message: "fatal message",
|
||||
Pairs: map[string]any{"fatal": "test fatal"},
|
||||
}
|
||||
|
||||
logger.Fatal(msg)
|
||||
|
||||
logOutput := output.String()
|
||||
assert.NotEmpty(logOutput)
|
||||
|
||||
var loggedMessage map[string]any
|
||||
err := json.Unmarshal([]byte(logOutput), &loggedMessage)
|
||||
assert.NoError(err)
|
||||
assert.Equal("fatal", loggedMessage["level"])
|
||||
assert.Equal("fatal message", loggedMessage["message"])
|
||||
assert.Equal("test fatal", loggedMessage["fatal"])
|
||||
}
|
||||
|
||||
func (suite *LoggerTestSuite) Test_SetFieldName() {
|
||||
// Save original field names
|
||||
originalFieldNames := make(map[string]string)
|
||||
for k, v := range fieldNames {
|
||||
originalFieldNames[k] = v
|
||||
}
|
||||
|
||||
// Restore original field names after test
|
||||
defer func() {
|
||||
for k, v := range originalFieldNames {
|
||||
fieldNames[k] = v
|
||||
}
|
||||
}()
|
||||
|
||||
logger := New()
|
||||
|
||||
// Test setting custom field names
|
||||
logger.SetFieldName("timestamp", "time")
|
||||
logger.SetFieldName("level", "severity")
|
||||
logger.SetFieldName("message", "msg")
|
||||
|
||||
output := &bytes.Buffer{}
|
||||
logger.SetOutput(output)
|
||||
|
||||
msg := &LogMessage{
|
||||
Message: "test message",
|
||||
}
|
||||
|
||||
logger.Info(msg)
|
||||
|
||||
logOutput := output.String()
|
||||
assert.NotEmpty(logOutput)
|
||||
|
||||
var loggedMessage map[string]any
|
||||
err := json.Unmarshal([]byte(logOutput), &loggedMessage)
|
||||
assert.NoError(err)
|
||||
|
||||
// Check that custom field names are used
|
||||
assert.Contains(loggedMessage, "time")
|
||||
assert.Contains(loggedMessage, "severity")
|
||||
assert.Contains(loggedMessage, "msg")
|
||||
assert.Equal("info", loggedMessage["severity"])
|
||||
assert.Equal("test message", loggedMessage["msg"])
|
||||
}
|
||||
|
||||
func (suite *LoggerTestSuite) Test_SetShowCaller() {
|
||||
output := &bytes.Buffer{}
|
||||
logger := New().SetOutput(output).SetShowCaller(true)
|
||||
|
||||
msg := &LogMessage{
|
||||
Message: "test message with caller",
|
||||
}
|
||||
|
||||
logger.Info(msg)
|
||||
|
||||
logOutput := output.String()
|
||||
assert.NotEmpty(logOutput)
|
||||
|
||||
var loggedMessage map[string]any
|
||||
err := json.Unmarshal([]byte(logOutput), &loggedMessage)
|
||||
assert.NoError(err)
|
||||
|
||||
// Check that caller information is included
|
||||
assert.Contains(loggedMessage, "caller")
|
||||
caller, ok := loggedMessage["caller"].(string)
|
||||
assert.True(ok)
|
||||
assert.Contains(caller, "logger_test.go:")
|
||||
}
|
||||
|
||||
func (suite *LoggerTestSuite) Test_GetCaller() {
|
||||
// Test getCaller function indirectly through SetShowCaller
|
||||
output := &bytes.Buffer{}
|
||||
logger := New().SetOutput(output).SetShowCaller(true).SetMinLogLevel(LEVEL_DEBUG)
|
||||
|
||||
msg := &LogMessage{
|
||||
Message: "caller test",
|
||||
}
|
||||
|
||||
logger.Debug(msg)
|
||||
|
||||
logOutput := output.String()
|
||||
assert.NotEmpty(logOutput)
|
||||
|
||||
var loggedMessage map[string]any
|
||||
err := json.Unmarshal([]byte(logOutput), &loggedMessage)
|
||||
assert.NoError(err)
|
||||
|
||||
caller, exists := loggedMessage["caller"]
|
||||
assert.True(exists)
|
||||
assert.NotEmpty(caller)
|
||||
|
||||
// Caller should be in format "file:line"
|
||||
callerStr, ok := caller.(string)
|
||||
assert.True(ok)
|
||||
assert.Contains(callerStr, ":")
|
||||
}
|
||||
|
||||
func (suite *LoggerTestSuite) Test_LogWithNilPairs() {
|
||||
output := &bytes.Buffer{}
|
||||
logger := New().SetOutput(output)
|
||||
|
||||
msg := &LogMessage{
|
||||
Message: "message with nil pairs",
|
||||
Pairs: nil,
|
||||
}
|
||||
|
||||
logger.Info(msg)
|
||||
|
||||
logOutput := output.String()
|
||||
assert.NotEmpty(logOutput)
|
||||
|
||||
var loggedMessage map[string]any
|
||||
err := json.Unmarshal([]byte(logOutput), &loggedMessage)
|
||||
assert.NoError(err)
|
||||
assert.Equal("info", loggedMessage["level"])
|
||||
assert.Equal("message with nil pairs", loggedMessage["message"])
|
||||
}
|
||||
|
||||
func (suite *LoggerTestSuite) Test_LogLevelFiltering() {
|
||||
output := &bytes.Buffer{}
|
||||
logger := New().SetOutput(output).SetMinLogLevel(LEVEL_WARN)
|
||||
|
||||
// These should not produce output (below minimum level)
|
||||
logger.Debug(&LogMessage{Message: "debug message"})
|
||||
logger.Info(&LogMessage{Message: "info message"})
|
||||
|
||||
debugInfoOutput := output.String()
|
||||
assert.Empty(debugInfoOutput)
|
||||
|
||||
// These should produce output (at or above minimum level)
|
||||
logger.Warn(&LogMessage{Message: "warn message"})
|
||||
warnOutput := output.String()
|
||||
assert.NotEmpty(warnOutput)
|
||||
assert.Contains(warnOutput, "warn message")
|
||||
|
||||
output.Reset()
|
||||
logger.Error(&LogMessage{Message: "error message"})
|
||||
errorOutput := output.String()
|
||||
assert.NotEmpty(errorOutput)
|
||||
assert.Contains(errorOutput, "error message")
|
||||
|
||||
output.Reset()
|
||||
logger.Fatal(&LogMessage{Message: "fatal message"})
|
||||
fatalOutput := output.String()
|
||||
assert.NotEmpty(fatalOutput)
|
||||
assert.Contains(fatalOutput, "fatal message")
|
||||
}
|
||||
|
||||
+2
-2
@@ -3,7 +3,7 @@ package gql
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
@@ -15,7 +15,7 @@ func StartMockServer() *httptest.Server {
|
||||
// Create your handler
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Your existing handler code
|
||||
bodyBytes, err := ioutil.ReadAll(r.Body)
|
||||
bodyBytes, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
cache "github.com/lukaszraczylo/go-simple-graphql/cache"
|
||||
)
|
||||
|
||||
// Performance regression tests to ensure optimizations maintain their benefits
|
||||
func TestPerformanceRegression(t *testing.T) {
|
||||
// Test hash performance
|
||||
t.Run("HashPerformance", func(t *testing.T) {
|
||||
query := &Query{
|
||||
Query: "query GetUser($id: ID!) { user(id: $id) { id name email } }",
|
||||
Variables: map[string]interface{}{
|
||||
"id": "12345",
|
||||
},
|
||||
JsonQuery: []byte(`{"query":"query GetUser($id: ID!) { user(id: $id) { id name email } }","variables":{"id":"12345"}}`),
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
for i := 0; i < 10000; i++ {
|
||||
_ = calculateHash(query)
|
||||
}
|
||||
duration := time.Since(start)
|
||||
|
||||
// Should complete 10k hashes in under 25ms (accounts for race detection + coverage overhead)
|
||||
if duration > 25*time.Millisecond {
|
||||
t.Errorf("Hash performance regression: 10k hashes took %v, expected < 25ms", duration)
|
||||
}
|
||||
})
|
||||
|
||||
// Test buffer pool performance
|
||||
t.Run("BufferPoolPerformance", func(t *testing.T) {
|
||||
start := time.Now()
|
||||
for i := 0; i < 10000; i++ {
|
||||
buf := getBuffer(1024)
|
||||
buf.WriteString("test data")
|
||||
putBuffer(buf)
|
||||
}
|
||||
duration := time.Since(start)
|
||||
|
||||
// Should complete 10k buffer operations in under 25ms (accounts for race detection + coverage overhead)
|
||||
if duration > 25*time.Millisecond {
|
||||
t.Errorf("Buffer pool performance regression: 10k operations took %v, expected < 25ms", duration)
|
||||
}
|
||||
})
|
||||
|
||||
// Test cache performance
|
||||
t.Run("CachePerformance", func(t *testing.T) {
|
||||
c := cache.New(5 * time.Second) // Use shorter TTL for tests
|
||||
defer c.Stop()
|
||||
|
||||
testData := make([]byte, 512) // Small data that won't be compressed
|
||||
for i := range testData {
|
||||
testData[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
for i := 0; i < 10000; i++ {
|
||||
key := "key" + string(rune(i%100))
|
||||
c.Set(key, testData, time.Minute)
|
||||
_, _ = c.Get(key)
|
||||
}
|
||||
duration := time.Since(start)
|
||||
|
||||
// Should complete 10k cache operations in under 50ms
|
||||
if duration > 50*time.Millisecond {
|
||||
t.Errorf("Cache performance regression: 10k operations took %v, expected < 50ms", duration)
|
||||
}
|
||||
})
|
||||
|
||||
// Test processFlags performance
|
||||
t.Run("ProcessFlagsPerformance", func(t *testing.T) {
|
||||
variables := map[string]interface{}{
|
||||
"id": "123",
|
||||
"gqlcache": true,
|
||||
"gqlretries": true,
|
||||
}
|
||||
headers := map[string]interface{}{}
|
||||
|
||||
start := time.Now()
|
||||
for i := 0; i < 10000; i++ {
|
||||
_, _, _ = processFlags(variables, headers)
|
||||
}
|
||||
duration := time.Since(start)
|
||||
|
||||
// Should complete 10k flag processing operations in under 30ms (accounts for race detection + coverage overhead)
|
||||
if duration > 30*time.Millisecond {
|
||||
t.Errorf("ProcessFlags performance regression: 10k operations took %v, expected < 30ms", duration)
|
||||
}
|
||||
})
|
||||
|
||||
// Test query compilation performance
|
||||
t.Run("QueryCompilationPerformance", func(t *testing.T) {
|
||||
client := CreateTestClient()
|
||||
|
||||
query := "query GetUser($id: ID!) { user(id: $id) { id name email } }"
|
||||
variables := map[string]interface{}{"id": "123"}
|
||||
|
||||
start := time.Now()
|
||||
for i := 0; i < 1000; i++ {
|
||||
_ = client.compileQuery(query, variables)
|
||||
}
|
||||
duration := time.Since(start)
|
||||
|
||||
// Should complete 1k query compilations in under 50ms
|
||||
if duration > 50*time.Millisecond {
|
||||
t.Errorf("Query compilation performance regression: 1k operations took %v, expected < 50ms", duration)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Benchmark comprehensive workflow to ensure no regression
|
||||
func BenchmarkComprehensiveWorkflow(b *testing.B) {
|
||||
client := CreateTestClient()
|
||||
|
||||
query := "query GetUser($id: ID!) { user(id: $id) { id name email profile { avatar bio } } }"
|
||||
variables := map[string]interface{}{
|
||||
"id": "123",
|
||||
"gqlcache": true,
|
||||
"gqlretries": true,
|
||||
}
|
||||
headers := map[string]interface{}{}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Process flags
|
||||
enableCache, enableRetries, cleanedVariables := processFlags(variables, headers)
|
||||
|
||||
// Compile query
|
||||
compiledQuery := client.compileQuery(query, cleanedVariables)
|
||||
|
||||
// Calculate hash
|
||||
if enableCache {
|
||||
_ = calculateHash(compiledQuery)
|
||||
}
|
||||
|
||||
// Use variables to avoid optimization
|
||||
_ = enableRetries
|
||||
}
|
||||
}
|
||||
|
||||
// Memory allocation benchmark
|
||||
func BenchmarkMemoryAllocations(b *testing.B) {
|
||||
client := CreateTestClient()
|
||||
|
||||
query := "query GetUser($id: ID!) { user(id: $id) { id name } }"
|
||||
variables := map[string]interface{}{"id": "123"}
|
||||
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
compiledQuery := client.compileQuery(query, variables)
|
||||
_ = calculateHash(compiledQuery)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
// Tiered buffer pools for different size requirements
|
||||
smallBufferPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
// Small buffers for typical GraphQL queries (1KB)
|
||||
b := bytes.NewBuffer(make([]byte, 0, 1024))
|
||||
return b
|
||||
},
|
||||
}
|
||||
|
||||
mediumBufferPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
// Medium buffers for larger queries (4KB)
|
||||
b := bytes.NewBuffer(make([]byte, 0, 4096))
|
||||
return b
|
||||
},
|
||||
}
|
||||
|
||||
largeBufferPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
// Large buffers for very large queries (16KB)
|
||||
b := bytes.NewBuffer(make([]byte, 0, 16384))
|
||||
return b
|
||||
},
|
||||
}
|
||||
|
||||
// Backward compatibility - use medium pool as default
|
||||
bufferPool *sync.Pool
|
||||
|
||||
// Pre-allocate error maps to reduce allocations
|
||||
errPairsPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make(map[string]interface{}, 1)
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
bufferPool = &mediumBufferPool
|
||||
}
|
||||
|
||||
// getBuffer returns an appropriately sized buffer based on estimated size
|
||||
func getBuffer(estimatedSize int) *bytes.Buffer {
|
||||
switch {
|
||||
case estimatedSize <= 1024:
|
||||
return smallBufferPool.Get().(*bytes.Buffer)
|
||||
case estimatedSize <= 4096:
|
||||
return mediumBufferPool.Get().(*bytes.Buffer)
|
||||
default:
|
||||
return largeBufferPool.Get().(*bytes.Buffer)
|
||||
}
|
||||
}
|
||||
|
||||
// putBuffer returns a buffer to the appropriate pool based on its capacity
|
||||
func putBuffer(buf *bytes.Buffer) {
|
||||
buf.Reset()
|
||||
capacity := buf.Cap()
|
||||
|
||||
switch {
|
||||
case capacity <= 1024:
|
||||
smallBufferPool.Put(buf)
|
||||
case capacity <= 4096:
|
||||
mediumBufferPool.Put(buf)
|
||||
default:
|
||||
largeBufferPool.Put(buf)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
@@ -11,61 +10,123 @@ import (
|
||||
)
|
||||
|
||||
func (b *BaseClient) convertToJSON(v any) []byte {
|
||||
// Reuse buffer to reduce allocations
|
||||
buf := bufferPool.Get().(*bytes.Buffer)
|
||||
defer bufferPool.Put(buf)
|
||||
buf.Reset()
|
||||
// Estimate size based on query structure for better buffer selection
|
||||
estimatedSize := 512 // Base size
|
||||
if query, ok := v.(*Query); ok {
|
||||
estimatedSize += len(query.Query)
|
||||
if query.Variables != nil {
|
||||
estimatedSize += len(query.Variables) * 50 // Rough estimate per variable
|
||||
}
|
||||
}
|
||||
|
||||
// Use json.Marshal to avoid adding extra newline
|
||||
jsonData, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
buf := getBuffer(estimatedSize)
|
||||
defer putBuffer(buf)
|
||||
|
||||
// Use json.NewEncoder for better performance with buffers
|
||||
enc := json.NewEncoder(buf)
|
||||
enc.SetEscapeHTML(false) // Reduce unnecessary escaping
|
||||
|
||||
if err := enc.Encode(v); err != nil {
|
||||
errPairs := errPairsPool.Get().(map[string]interface{})
|
||||
errPairs["error"] = err.Error()
|
||||
b.Logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "Can't convert to JSON",
|
||||
Pairs: map[string]interface{}{"error": err.Error()},
|
||||
Pairs: errPairs,
|
||||
})
|
||||
errPairsPool.Put(errPairs)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Copy the bytes to the buffer
|
||||
buf.Write(jsonData)
|
||||
// Get the buffer bytes directly, trimming the trailing newline
|
||||
bytes := buf.Bytes()
|
||||
if len(bytes) > 0 && bytes[len(bytes)-1] == '\n' {
|
||||
bytes = bytes[:len(bytes)-1]
|
||||
}
|
||||
|
||||
// Copy the bytes to a new slice before returning
|
||||
result := make([]byte, buf.Len())
|
||||
copy(result, buf.Bytes())
|
||||
// Make a copy of the bytes since the buffer will be reused
|
||||
result := make([]byte, len(bytes))
|
||||
copy(result, bytes)
|
||||
return result
|
||||
}
|
||||
|
||||
func processFlags(variables map[string]interface{}, headers map[string]interface{}) (enableCache, enableRetries bool, cleanedVariables map[string]interface{}) {
|
||||
// Start with original variables
|
||||
cleanedVariables = variables
|
||||
|
||||
// Check headers first
|
||||
enableCache, _ = goutil.ToBool(searchForKeysInMapStringInterface(headers, "gqlcache"))
|
||||
enableRetries, _ = goutil.ToBool(searchForKeysInMapStringInterface(headers, "gqlretries"))
|
||||
|
||||
// Check variables and clean them if needed
|
||||
if variables != nil {
|
||||
varEnableCache, _ := goutil.ToBool(searchForKeysInMapStringInterface(variables, "gqlcache"))
|
||||
varEnableRetries, _ := goutil.ToBool(searchForKeysInMapStringInterface(variables, "gqlretries"))
|
||||
|
||||
enableCache = enableCache || varEnableCache
|
||||
enableRetries = enableRetries || varEnableRetries
|
||||
|
||||
// Clean variables if flags are present (regardless of their value)
|
||||
_, hasCacheFlag := variables["gqlcache"]
|
||||
_, hasRetriesFlag := variables["gqlretries"]
|
||||
|
||||
if hasCacheFlag || hasRetriesFlag {
|
||||
cleanedVariables = make(map[string]interface{})
|
||||
for k, v := range variables {
|
||||
if k != "gqlcache" && k != "gqlretries" {
|
||||
cleanedVariables[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return enableCache, enableRetries, cleanedVariables
|
||||
}
|
||||
|
||||
func (b *BaseClient) compileQuery(queryPartials ...any) *Query {
|
||||
var query string
|
||||
var variables map[string]interface{}
|
||||
for _, partial := range queryPartials {
|
||||
switch val := partial.(type) {
|
||||
case string:
|
||||
query = val
|
||||
case map[string]interface{}:
|
||||
variables = val
|
||||
|
||||
// Pre-allocate the query with an estimated size
|
||||
if len(queryPartials) > 0 {
|
||||
if str, ok := queryPartials[0].(string); ok {
|
||||
query = str
|
||||
}
|
||||
}
|
||||
|
||||
// Only allocate variables map if we have more than one partial
|
||||
if len(queryPartials) > 1 {
|
||||
if vars, ok := queryPartials[1].(map[string]interface{}); ok {
|
||||
variables = vars
|
||||
}
|
||||
}
|
||||
|
||||
if query == "" {
|
||||
errPairs := errPairsPool.Get().(map[string]interface{})
|
||||
errPairs["error"] = "query is empty"
|
||||
b.Logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "Can't compile query",
|
||||
Pairs: map[string]interface{}{"error": "query is empty"},
|
||||
Pairs: errPairs,
|
||||
})
|
||||
errPairsPool.Put(errPairs)
|
||||
return nil
|
||||
}
|
||||
|
||||
jsonQuery := b.convertToJSON(&Query{Query: query, Variables: variables})
|
||||
return &Query{
|
||||
// Construct query object once
|
||||
q := &Query{
|
||||
Query: query,
|
||||
Variables: variables,
|
||||
JsonQuery: jsonQuery,
|
||||
}
|
||||
q.JsonQuery = b.convertToJSON(q)
|
||||
return q
|
||||
}
|
||||
|
||||
func (b *BaseClient) Query(query string, variables map[string]interface{}, headers map[string]interface{}) (any, error) {
|
||||
compiledQuery := b.compileQuery(query, variables)
|
||||
if compiledQuery.JsonQuery == nil {
|
||||
// Process flags before compilation to avoid recompilation
|
||||
enableCache, enableRetries, cleanedVariables := processFlags(variables, headers)
|
||||
|
||||
// Compile query once with cleaned variables
|
||||
compiledQuery := b.compileQuery(query, cleanedVariables)
|
||||
if compiledQuery == nil || compiledQuery.JsonQuery == nil {
|
||||
b.Logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "Can't compile query",
|
||||
Pairs: map[string]interface{}{"error": "query is empty"},
|
||||
@@ -77,11 +138,6 @@ func (b *BaseClient) Query(query string, variables map[string]interface{}, heade
|
||||
Pairs: map[string]interface{}{"query": compiledQuery},
|
||||
})
|
||||
|
||||
enableCache, enableRetries, recompileRequired := compiledQuery.parseHeadersAndVariables(headers)
|
||||
if recompileRequired {
|
||||
compiledQuery = b.compileQuery(query, variables)
|
||||
}
|
||||
|
||||
var queryHash string
|
||||
if (enableCache || b.cache_global) && strutil.HasPrefix(compiledQuery.Query, "query") {
|
||||
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||
@@ -126,23 +182,3 @@ func (b *BaseClient) Query(query string, variables map[string]interface{}, heade
|
||||
|
||||
return b.decodeResponse(rv)
|
||||
}
|
||||
|
||||
func (q *Query) parseHeadersAndVariables(headers map[string]interface{}) (enableCache, enableRetries, recompileRequired bool) {
|
||||
enableCache, _ = goutil.ToBool(searchForKeysInMapStringInterface(headers, "gqlcache"))
|
||||
enableRetries, _ = goutil.ToBool(searchForKeysInMapStringInterface(headers, "gqlretries"))
|
||||
|
||||
if q.Variables != nil {
|
||||
varEnableCache, _ := goutil.ToBool(searchForKeysInMapStringInterface(q.Variables, "gqlcache"))
|
||||
varEnableRetries, _ := goutil.ToBool(searchForKeysInMapStringInterface(q.Variables, "gqlretries"))
|
||||
enableCache = enableCache || varEnableCache
|
||||
enableRetries = enableRetries || varEnableRetries
|
||||
|
||||
if varEnableCache || varEnableRetries {
|
||||
delete(q.Variables, "gqlcache")
|
||||
delete(q.Variables, "gqlretries")
|
||||
recompileRequired = true
|
||||
}
|
||||
}
|
||||
|
||||
return enableCache, enableRetries, recompileRequired
|
||||
}
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type QueryOptimizationTestSuite struct {
|
||||
suite.Suite
|
||||
client *BaseClient
|
||||
}
|
||||
|
||||
func (suite *QueryOptimizationTestSuite) SetupTest() {
|
||||
suite.client = CreateTestClient()
|
||||
}
|
||||
|
||||
func (suite *QueryOptimizationTestSuite) TearDownTest() {
|
||||
// No need to stop shared cache - it will be cleaned up globally
|
||||
}
|
||||
|
||||
func (suite *QueryOptimizationTestSuite) TestProcessFlags() {
|
||||
tests := []struct {
|
||||
name string
|
||||
variables map[string]interface{}
|
||||
headers map[string]interface{}
|
||||
expectedCache bool
|
||||
expectedRetries bool
|
||||
expectedVarCount int
|
||||
}{
|
||||
{
|
||||
name: "No flags",
|
||||
variables: map[string]interface{}{"id": "123"},
|
||||
headers: map[string]interface{}{},
|
||||
expectedCache: false,
|
||||
expectedRetries: false,
|
||||
expectedVarCount: 1,
|
||||
},
|
||||
{
|
||||
name: "Cache flag in headers",
|
||||
variables: map[string]interface{}{"id": "123"},
|
||||
headers: map[string]interface{}{"gqlcache": true},
|
||||
expectedCache: true,
|
||||
expectedRetries: false,
|
||||
expectedVarCount: 1,
|
||||
},
|
||||
{
|
||||
name: "Retries flag in headers",
|
||||
variables: map[string]interface{}{"id": "123"},
|
||||
headers: map[string]interface{}{"gqlretries": true},
|
||||
expectedCache: false,
|
||||
expectedRetries: true,
|
||||
expectedVarCount: 1,
|
||||
},
|
||||
{
|
||||
name: "Cache flag in variables",
|
||||
variables: map[string]interface{}{"id": "123", "gqlcache": true},
|
||||
headers: map[string]interface{}{},
|
||||
expectedCache: true,
|
||||
expectedRetries: false,
|
||||
expectedVarCount: 1, // gqlcache should be removed
|
||||
},
|
||||
{
|
||||
name: "Retries flag in variables",
|
||||
variables: map[string]interface{}{"id": "123", "gqlretries": true},
|
||||
headers: map[string]interface{}{},
|
||||
expectedCache: false,
|
||||
expectedRetries: true,
|
||||
expectedVarCount: 1, // gqlretries should be removed
|
||||
},
|
||||
{
|
||||
name: "Both flags in variables",
|
||||
variables: map[string]interface{}{"id": "123", "gqlcache": true, "gqlretries": true},
|
||||
headers: map[string]interface{}{},
|
||||
expectedCache: true,
|
||||
expectedRetries: true,
|
||||
expectedVarCount: 1, // both flags should be removed
|
||||
},
|
||||
{
|
||||
name: "Flags in both headers and variables",
|
||||
variables: map[string]interface{}{"id": "123", "gqlcache": false},
|
||||
headers: map[string]interface{}{"gqlcache": true, "gqlretries": true},
|
||||
expectedCache: true, // header value OR variable value
|
||||
expectedRetries: true,
|
||||
expectedVarCount: 1, // gqlcache should be removed from variables
|
||||
},
|
||||
{
|
||||
name: "Nil variables",
|
||||
variables: nil,
|
||||
headers: map[string]interface{}{"gqlcache": true},
|
||||
expectedCache: true,
|
||||
expectedRetries: false,
|
||||
expectedVarCount: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
suite.Run(tt.name, func() {
|
||||
enableCache, enableRetries, cleanedVariables := processFlags(tt.variables, tt.headers)
|
||||
|
||||
suite.Equal(tt.expectedCache, enableCache, "Cache flag mismatch")
|
||||
suite.Equal(tt.expectedRetries, enableRetries, "Retries flag mismatch")
|
||||
|
||||
if tt.expectedVarCount == 0 {
|
||||
suite.Nil(cleanedVariables, "Variables should be nil")
|
||||
} else {
|
||||
suite.Len(cleanedVariables, tt.expectedVarCount, "Variable count mismatch")
|
||||
suite.NotContains(cleanedVariables, "gqlcache", "gqlcache should be removed")
|
||||
suite.NotContains(cleanedVariables, "gqlretries", "gqlretries should be removed")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *QueryOptimizationTestSuite) TestDoubleCompilationFix() {
|
||||
// Test that query is only compiled once even with flags in variables
|
||||
query := "query GetUser($id: ID!) { user(id: $id) { id name } }"
|
||||
variables := map[string]interface{}{
|
||||
"id": "123",
|
||||
"gqlcache": true,
|
||||
"gqlretries": true,
|
||||
}
|
||||
|
||||
// This should not cause double compilation
|
||||
compiledQuery := suite.client.compileQuery(query, variables)
|
||||
suite.NotNil(compiledQuery)
|
||||
suite.Equal(query, compiledQuery.Query)
|
||||
|
||||
// Variables should still contain the flags at this point (they're cleaned in Query method)
|
||||
suite.Contains(compiledQuery.Variables, "gqlcache")
|
||||
suite.Contains(compiledQuery.Variables, "gqlretries")
|
||||
}
|
||||
|
||||
func (suite *QueryOptimizationTestSuite) TestQueryMethodOptimization() {
|
||||
// Test that processFlags works correctly without making HTTP requests
|
||||
variables := map[string]interface{}{
|
||||
"id": "123",
|
||||
"gqlcache": true,
|
||||
"gqlretries": true,
|
||||
}
|
||||
headers := map[string]interface{}{}
|
||||
|
||||
// Test processFlags directly
|
||||
enableCache, enableRetries, cleanedVariables := processFlags(variables, headers)
|
||||
|
||||
suite.True(enableCache, "Cache should be enabled")
|
||||
suite.True(enableRetries, "Retries should be enabled")
|
||||
suite.Len(cleanedVariables, 1, "Should have only id variable")
|
||||
suite.Equal("123", cleanedVariables["id"], "ID should be preserved")
|
||||
suite.NotContains(cleanedVariables, "gqlcache", "gqlcache should be removed")
|
||||
suite.NotContains(cleanedVariables, "gqlretries", "gqlretries should be removed")
|
||||
}
|
||||
|
||||
// Benchmark the optimization impact
|
||||
func BenchmarkQueryCompilation(b *testing.B) {
|
||||
client := CreateTestClient()
|
||||
|
||||
query := "query GetUser($id: ID!) { user(id: $id) { id name email profile { avatar bio } } }"
|
||||
|
||||
b.Run("WithoutFlags", func(b *testing.B) {
|
||||
variables := map[string]interface{}{"id": "123"}
|
||||
headers := map[string]interface{}{}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
processFlags(variables, headers)
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("WithFlags", func(b *testing.B) {
|
||||
variables := map[string]interface{}{
|
||||
"id": "123",
|
||||
"gqlcache": true,
|
||||
"gqlretries": true,
|
||||
}
|
||||
headers := map[string]interface{}{}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
processFlags(variables, headers)
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("CompileQuery", func(b *testing.B) {
|
||||
variables := map[string]interface{}{"id": "123"}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
client.compileQuery(query, variables)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestQueryOptimizationSuite(t *testing.T) {
|
||||
suite.Run(t, new(QueryOptimizationTestSuite))
|
||||
}
|
||||
+245
@@ -182,3 +182,248 @@ func (suite *Tests) TestBaseClient_Query() {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *Tests) TestBaseClient_convertToJSON_errors() {
|
||||
suite.T().Run("should handle JSON encoding errors", func(t *testing.T) {
|
||||
b := NewConnection()
|
||||
|
||||
// Test with a value that can't be JSON encoded (function)
|
||||
result := b.convertToJSON(func() {})
|
||||
assert.Nil(result)
|
||||
})
|
||||
|
||||
suite.T().Run("should handle Query type with variables", func(t *testing.T) {
|
||||
b := NewConnection()
|
||||
|
||||
query := &Query{
|
||||
Query: "query { test }",
|
||||
Variables: map[string]interface{}{
|
||||
"var1": "value1",
|
||||
"var2": 123,
|
||||
},
|
||||
}
|
||||
|
||||
result := b.convertToJSON(query)
|
||||
assert.NotNil(result)
|
||||
assert.Contains(string(result), "query { test }")
|
||||
assert.Contains(string(result), "var1")
|
||||
assert.Contains(string(result), "value1")
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestBaseClient_compileQuery_errors() {
|
||||
suite.T().Run("should handle empty query", func(t *testing.T) {
|
||||
b := NewConnection()
|
||||
|
||||
result := b.compileQuery("")
|
||||
assert.Nil(result)
|
||||
})
|
||||
|
||||
suite.T().Run("should handle no arguments", func(t *testing.T) {
|
||||
b := NewConnection()
|
||||
|
||||
result := b.compileQuery()
|
||||
assert.Nil(result)
|
||||
})
|
||||
|
||||
suite.T().Run("should handle invalid variable type", func(t *testing.T) {
|
||||
b := NewConnection()
|
||||
|
||||
result := b.compileQuery("query { test }", "invalid_variables")
|
||||
assert.NotNil(result)
|
||||
assert.Equal("query { test }", result.Query)
|
||||
assert.Nil(result.Variables)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) Test_processFlags() {
|
||||
tests := []struct {
|
||||
name string
|
||||
variables map[string]interface{}
|
||||
headers map[string]interface{}
|
||||
wantCache bool
|
||||
wantRetries bool
|
||||
wantCleanedVars map[string]interface{}
|
||||
}{
|
||||
{
|
||||
name: "cache enabled in headers",
|
||||
variables: nil,
|
||||
headers: map[string]interface{}{
|
||||
"gqlcache": true,
|
||||
},
|
||||
wantCache: true,
|
||||
wantRetries: false,
|
||||
wantCleanedVars: nil,
|
||||
},
|
||||
{
|
||||
name: "retries enabled in headers",
|
||||
variables: nil,
|
||||
headers: map[string]interface{}{
|
||||
"gqlretries": true,
|
||||
},
|
||||
wantCache: false,
|
||||
wantRetries: true,
|
||||
wantCleanedVars: nil,
|
||||
},
|
||||
{
|
||||
name: "cache enabled in variables",
|
||||
variables: map[string]interface{}{
|
||||
"gqlcache": true,
|
||||
"test": "value",
|
||||
},
|
||||
headers: nil,
|
||||
wantCache: true,
|
||||
wantRetries: false,
|
||||
wantCleanedVars: map[string]interface{}{
|
||||
"test": "value",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "retries enabled in variables",
|
||||
variables: map[string]interface{}{
|
||||
"gqlretries": true,
|
||||
"test": "value",
|
||||
},
|
||||
headers: nil,
|
||||
wantCache: false,
|
||||
wantRetries: true,
|
||||
wantCleanedVars: map[string]interface{}{
|
||||
"test": "value",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "both flags in variables",
|
||||
variables: map[string]interface{}{
|
||||
"gqlcache": true,
|
||||
"gqlretries": false,
|
||||
"test": "value",
|
||||
},
|
||||
headers: nil,
|
||||
wantCache: true,
|
||||
wantRetries: false,
|
||||
wantCleanedVars: map[string]interface{}{
|
||||
"test": "value",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "flags in both headers and variables",
|
||||
variables: map[string]interface{}{
|
||||
"gqlcache": false,
|
||||
"test": "value",
|
||||
},
|
||||
headers: map[string]interface{}{
|
||||
"gqlretries": true,
|
||||
},
|
||||
wantCache: false,
|
||||
wantRetries: true,
|
||||
wantCleanedVars: map[string]interface{}{
|
||||
"test": "value",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no flags",
|
||||
variables: map[string]interface{}{"test": "value"},
|
||||
headers: nil,
|
||||
wantCache: false,
|
||||
wantRetries: false,
|
||||
wantCleanedVars: map[string]interface{}{"test": "value"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
suite.T().Run(tt.name, func(t *testing.T) {
|
||||
gotCache, gotRetries, gotCleanedVars := processFlags(tt.variables, tt.headers)
|
||||
assert.Equal(tt.wantCache, gotCache)
|
||||
assert.Equal(tt.wantRetries, gotRetries)
|
||||
assert.Equal(tt.wantCleanedVars, gotCleanedVars)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *Tests) TestBaseClient_Query_caching() {
|
||||
suite.T().Run("should use cache for query operations", func(t *testing.T) {
|
||||
b := NewConnection()
|
||||
b.SetEndpoint(mockServer.URL)
|
||||
b.SetHTTPClient(mockServer.Client())
|
||||
|
||||
query := "query { viewer { login } }"
|
||||
variables := map[string]interface{}{
|
||||
"gqlcache": true,
|
||||
}
|
||||
|
||||
// First call should hit the server
|
||||
result1, err := b.Query(query, variables, nil)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result1)
|
||||
|
||||
// Second call should use cache
|
||||
result2, err := b.Query(query, variables, nil)
|
||||
assert.NoError(err)
|
||||
assert.Equal(result1, result2)
|
||||
})
|
||||
|
||||
suite.T().Run("should not cache mutations", func(t *testing.T) {
|
||||
b := NewConnection()
|
||||
b.SetEndpoint(mockServer.URL)
|
||||
b.SetHTTPClient(mockServer.Client())
|
||||
|
||||
mutation := "mutation { updateUser { id } }"
|
||||
variables := map[string]interface{}{
|
||||
"gqlcache": true,
|
||||
}
|
||||
|
||||
// Mutations should not be cached even with cache flag
|
||||
_, err := b.Query(mutation, variables, nil)
|
||||
assert.Error(err) // Mock server doesn't handle mutations
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestBaseClient_Query_retries() {
|
||||
suite.T().Run("should handle retries flag", func(t *testing.T) {
|
||||
b := NewConnection()
|
||||
b.SetEndpoint(mockServer.URL)
|
||||
b.SetHTTPClient(mockServer.Client())
|
||||
|
||||
query := "query { viewer { login } }"
|
||||
variables := map[string]interface{}{
|
||||
"gqlretries": true,
|
||||
}
|
||||
|
||||
result, err := b.Query(query, variables, nil)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestBaseClient_Query_globalCache() {
|
||||
suite.T().Run("should respect global cache setting", func(t *testing.T) {
|
||||
b := NewConnection()
|
||||
b.SetEndpoint(mockServer.URL)
|
||||
b.SetHTTPClient(mockServer.Client())
|
||||
b.cache_global = true
|
||||
|
||||
query := "query { viewer { login } }"
|
||||
|
||||
// Should use cache even without explicit flag
|
||||
result1, err := b.Query(query, nil, nil)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result1)
|
||||
|
||||
result2, err := b.Query(query, nil, nil)
|
||||
assert.NoError(err)
|
||||
assert.Equal(result1, result2)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestBaseClient_Query_emptyQuery() {
|
||||
suite.T().Run("should handle empty query", func(t *testing.T) {
|
||||
b := NewConnection()
|
||||
b.SetEndpoint(mockServer.URL)
|
||||
b.SetHTTPClient(mockServer.Client())
|
||||
|
||||
result, err := b.Query("", nil, nil)
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
assert.Contains(err.Error(), "can't compile query")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_main(t *testing.T) {
|
||||
// Test that main function doesn't panic during setup
|
||||
// The actual GraphQL calls will fail, but we want to test the setup code
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Errorf("main() panicked: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
// Call main() to get coverage - it will fail on network calls but that's expected
|
||||
// This tests the NewConnection() call and query compilation
|
||||
main()
|
||||
}
|
||||
|
||||
func Test_samplePackageExists(t *testing.T) {
|
||||
// This test ensures the sample package is properly structured
|
||||
// and can be imported/compiled
|
||||
|
||||
// Test passes if we reach this point without compilation errors
|
||||
}
|
||||
@@ -27,6 +27,9 @@ func (suite *Tests) TearDownSuite() {
|
||||
if mockServer != nil {
|
||||
mockServer.Close()
|
||||
}
|
||||
|
||||
// Cleanup shared test cache
|
||||
CleanupTestCache()
|
||||
}
|
||||
|
||||
func (suite *Tests) SetupTest() {
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
cache "github.com/lukaszraczylo/go-simple-graphql/cache"
|
||||
logging "github.com/lukaszraczylo/go-simple-graphql/logging"
|
||||
)
|
||||
|
||||
var (
|
||||
testCacheOnce sync.Once
|
||||
testCache *cache.Cache
|
||||
testLogger *logging.Logger
|
||||
)
|
||||
|
||||
// GetTestCache returns a shared cache instance for tests to reduce resource contention
|
||||
func GetTestCache() *cache.Cache {
|
||||
testCacheOnce.Do(func() {
|
||||
testCache = cache.New(5 * time.Second) // Short TTL for tests
|
||||
})
|
||||
return testCache
|
||||
}
|
||||
|
||||
// GetTestLogger returns a shared logger instance for tests
|
||||
func GetTestLogger() *logging.Logger {
|
||||
if testLogger == nil {
|
||||
testLogger = logging.New()
|
||||
testLogger.SetMinLogLevel(logging.LEVEL_ERROR)
|
||||
}
|
||||
return testLogger
|
||||
}
|
||||
|
||||
// CleanupTestCache stops the shared test cache - call this in TestMain if needed
|
||||
func CleanupTestCache() {
|
||||
if testCache != nil {
|
||||
testCache.Stop()
|
||||
}
|
||||
}
|
||||
|
||||
// CreateTestClient creates a test client with shared resources
|
||||
func CreateTestClient() *BaseClient {
|
||||
return &BaseClient{
|
||||
Logger: GetTestLogger(),
|
||||
cache: GetTestCache(),
|
||||
endpoint: "https://example.com/graphql",
|
||||
responseType: "mapstring",
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user