network/pkg/rqlite/util_test.go
anonpenguin23 a9844a1451 feat: add unit tests for gateway authentication and RQLite utilities
- Introduced comprehensive unit tests for the authentication service in the gateway, covering JWT generation, Base58 decoding, and signature verification for Ethereum and Solana.
- Added tests for RQLite cluster discovery functions, including host replacement logic and public IP validation.
- Implemented tests for RQLite utility functions, focusing on exponential backoff and data directory path resolution.
- Enhanced serverless engine tests to validate timeout handling and memory limits for WASM functions.
2025-12-31 12:26:31 +02:00

90 lines
2.3 KiB
Go

package rqlite
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestExponentialBackoff(t *testing.T) {
r := &RQLiteManager{}
baseDelay := 100 * time.Millisecond
maxDelay := 1 * time.Second
tests := []struct {
attempt int
expected time.Duration
}{
{0, 100 * time.Millisecond},
{1, 200 * time.Millisecond},
{2, 400 * time.Millisecond},
{3, 800 * time.Millisecond},
{4, 1000 * time.Millisecond}, // Maxed out
{10, 1000 * time.Millisecond}, // Maxed out
}
for _, tt := range tests {
got := r.exponentialBackoff(tt.attempt, baseDelay, maxDelay)
if got != tt.expected {
t.Errorf("exponentialBackoff(%d) = %v; want %v", tt.attempt, got, tt.expected)
}
}
}
func TestRQLiteDataDirPath(t *testing.T) {
// Test with explicit path
r := &RQLiteManager{dataDir: "/tmp/data"}
got, _ := r.rqliteDataDirPath()
expected := filepath.Join("/tmp/data", "rqlite")
if got != expected {
t.Errorf("rqliteDataDirPath() = %s; want %s", got, expected)
}
// Test with environment variable expansion
os.Setenv("TEST_DATA_DIR", "/tmp/env-data")
defer os.Unsetenv("TEST_DATA_DIR")
r = &RQLiteManager{dataDir: "$TEST_DATA_DIR"}
got, _ = r.rqliteDataDirPath()
expected = filepath.Join("/tmp/env-data", "rqlite")
if got != expected {
t.Errorf("rqliteDataDirPath() with env = %s; want %s", got, expected)
}
// Test with home directory expansion
r = &RQLiteManager{dataDir: "~/data"}
got, _ = r.rqliteDataDirPath()
home, _ := os.UserHomeDir()
expected = filepath.Join(home, "data", "rqlite")
if got != expected {
t.Errorf("rqliteDataDirPath() with ~ = %s; want %s", got, expected)
}
}
func TestHasExistingState(t *testing.T) {
r := &RQLiteManager{}
// Create a temp directory for testing
tmpDir, err := os.MkdirTemp("", "rqlite-test-*")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
// Test empty directory
if r.hasExistingState(tmpDir) {
t.Errorf("hasExistingState() = true; want false for empty dir")
}
// Test directory with a file
testFile := filepath.Join(tmpDir, "test.txt")
if err := os.WriteFile(testFile, []byte("data"), 0644); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
if !r.hasExistingState(tmpDir) {
t.Errorf("hasExistingState() = false; want true for non-empty dir")
}
}