Some checks failed
go: build dev binaries / cleanup (push) Successful in 4s
Codespell / Check for spelling errors (push) Failing after 3s
docker: build dev containers / build-rust-binaries (amd64, x86_64-unknown-linux-musl) (push) Failing after 3s
docker: build dev containers / build-dev-containers (push) Has been skipped
docker: build dev containers / build-rust-binaries (arm64, true, aarch64-unknown-linux-musl) (push) Failing after 3s
End to End / FUSE Mount (push) Failing after 2s
EC Integration Tests / EC Integration Tests (push) Failing after 30s
FUSE DLM Integration Tests / FUSE DLM Integration Tests (push) Failing after 3s
FUSE Volume Server Failover Tests / FUSE Volume Server Failover (push) Failing after 3s
FUSE Integration Tests / FUSE Integration Testing (push) Failing after 2s
FUSE P2P Peer Chunk Sharing Integration Tests / FUSE P2P Peer Chunk Sharing (push) Failing after 3s
go: build binary / Go Vet (push) Failing after 2s
go: build binary / Go Vet 32-bit (push) Failing after 3s
go: build binary / Build (push) Failing after 4s
go: build binary / Build cross-platform (freebsd/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (darwin/arm64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/arm64) (push) Failing after 3s
go: build binary / Test (push) Failing after 2s
go: build binary / Test 32-bit (push) Failing after 3s
helm: lint and test charts / lint-test (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (11) (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (17) (push) Failing after 3s
go: build dev binaries / build_dev_linux_windows (amd64, linux) (push) Failing after 2s
go: build dev binaries / build_dev_linux_windows (amd64, windows) (push) Failing after 3s
go: build dev binaries / build_dev_darwin (amd64, darwin) (push) Has been skipped
go: build dev binaries / build_dev_darwin (arm64, darwin) (push) Has been skipped
Java Client Unit Tests / Java Unit Tests (11) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (17) (push) Failing after 2s
Java Client Unit Tests / Java Unit Tests (21) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (8) (push) Failing after 3s
Kafka Quick Test (Load Test with Schema Registry) / Kafka Client Load Test (Quick) (push) Failing after 3s
Kafka Gateway Tests / Kafka Unit Tests (unit-tests-1) (push) Failing after 11s
Kafka Gateway Tests / Kafka Integration Tests (Critical) (integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka End-to-End Tests (with SMQ) (e2e-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Consumer Group Tests (Highly Isolated) (consumer-group-1) (push) Failing after 2s
Kafka Gateway Tests / Kafka Client Compatibility (with SMQ) (client-compat-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka SMQ Integration Tests (Full Stack) (smq-integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Protocol Tests (Isolated) (protocol-1) (push) Failing after 3s
KMS Tests / KMS Provider Integration Tests (push) Failing after 2s
KMS Tests / S3 KMS End-to-End Tests (push) Failing after 3s
Master Cold Start Tests / Master Cold Start Tests (push) Failing after 4s
Metadata Subscribe Integration Tests / Metadata Subscribe Integration Tests (push) Failing after 3s
Multi-Master Tests / Multi-Master Failover Tests (push) Failing after 3s
Performance / CPU and Heap Profile (push) Failing after 2s
Performance / S3 Read/Write Benchmark (rust volume) (push) Failing after 3s
pjdfstest POSIX Compliance / pjdfstest (push) Failing after 2s
Plugin Worker Integration Tests / Plugin Worker: erasure_coding (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: vacuum (push) Failing after 3s
Rust Volume Server Tests / Rust Integration Tests (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 1) (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 1) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 3) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Distributed Tests (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Performance Tests (push) Failing after 3s
Performance / Throughput Benchmark (go volume) (push) Failing after 3s
Performance / Throughput Benchmark (rust volume) (push) Failing after 2s
Performance / S3 Read/Write Benchmark (go volume) (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: volume_balance (push) Failing after 3s
PostgreSQL Gateway Tests / PostgreSQL Basic Tests (push) Failing after 3s
Rust Volume Server Tests / Rust Unit Tests (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 3) (push) Failing after 3s
Rust Plugin Worker Tests / Rust Plugin Worker Build and Unit Tests (push) Failing after 3s
rust: build dev volume server binaries / cleanup (push) Successful in 3s
S3 ETag and ACL Tests / S3 ETag + ACL Integration Tests (push) Failing after 3s
S3 IAM Integration Tests / IAM Unit Tests (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (advanced) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (basic) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (group) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (policy-enforcement) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (sts) (push) Failing after 4s
S3 Keycloak Integration Tests / S3 Keycloak Integration Tests (push) Failing after 3s
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.11) (push) Failing after 3s
S3 PyArrow Parquet Tests / Go Unit Tests (Implicit Directory) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.12) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-amd64, x86_64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-arm64, aarch64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-linux (linux-amd64, x86_64-unknown-linux-gnu) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.9) (push) Has been cancelled
S3 Proxy Signature Tests / S3 Proxy Signature Verification Tests (push) Has been cancelled
S3 SDK V2 Route Disambiguation Tests / S3 SDK V2 Routing Tests (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (quick) (push) Has been cancelled
S3 SSE Tests / S3 SSE Compatibility Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Metadata Persistence Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Copy Operations Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Multipart Upload Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Performance Test (push) Has been cancelled
S3 SSE Tests / S3 Volume Encryption Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (comprehensive) (push) Has been cancelled
Spark Integration Tests / Spark Integration Tests (push) Failing after 2s
Telemetry Integration Tests / telemetry-integration-test (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 1) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 1) (push) Failing after 3s
Ceph S3 tests / Basic S3 tests (KV store) (push) Has been cancelled
Ceph S3 tests / S3 Versioning & Object Lock tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Unit Tests (push) Failing after 3s
Ceph S3 tests / S3 CORS tests (push) Has been cancelled
Ceph S3 tests / SeaweedFS Custom S3 Copy tests (push) Has been cancelled
Ceph S3 tests / Basic S3 tests (SQL store) (push) Has been cancelled
test s3 over https using aws-cli / awscli-tests (push) Has been cancelled
TLS Rotation Integration Tests / TLS Rotation Integration Tests (push) Has been cancelled
Vacuum Integration Tests / Vacuum Integration Tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Variables Integration Tests (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (advanced-policy) (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (basic-policy) (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (contextual-policy) (push) Failing after 4s
S3 Policy Integration Tests / Trusted Proxy Detection Tests (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Shell Integration Tests (push) Failing after 3s
Samba on FUSE Integration / samba-integration (push) Failing after 2s
SFTP Integration Tests / SFTP Integration Testing (push) Failing after 3s
terraform: validate and test modules / fmt, validate, plan-level tests (push) Failing after 3s
terraform: validate and test modules / local cluster smoke test (real weed) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 3) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 2) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 2) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 3) (push) Failing after 5s
mount: windows conformance / WinFsp conformance (push) Has been cancelled
mount: windows / Mount on Windows (push) Has been cancelled
644 lines
17 KiB
Go
644 lines
17 KiB
Go
package schema
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/linkedin/goavro/v2"
|
|
)
|
|
|
|
// TestFullIntegration_AvroWorkflow tests the complete Avro workflow
|
|
func TestFullIntegration_AvroWorkflow(t *testing.T) {
|
|
// Create comprehensive mock schema registry
|
|
server := createMockSchemaRegistry(t)
|
|
defer server.Close()
|
|
|
|
// Create manager with realistic configuration
|
|
config := ManagerConfig{
|
|
RegistryURL: server.URL,
|
|
ValidationMode: ValidationPermissive,
|
|
EnableMirroring: false,
|
|
CacheTTL: "5m",
|
|
}
|
|
|
|
manager, err := NewManager(config)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create manager: %v", err)
|
|
}
|
|
|
|
// Test 1: Producer workflow - encode schematized message
|
|
t.Run("Producer_Workflow", func(t *testing.T) {
|
|
// Create realistic user data (with proper Avro union handling)
|
|
userData := map[string]interface{}{
|
|
"id": int32(12345),
|
|
"name": "Alice Johnson",
|
|
"email": map[string]interface{}{"string": "alice@example.com"}, // Avro union
|
|
"age": map[string]interface{}{"int": int32(28)}, // Avro union
|
|
"preferences": map[string]interface{}{
|
|
"Preferences": map[string]interface{}{ // Avro union with record type
|
|
"notifications": true,
|
|
"theme": "dark",
|
|
},
|
|
},
|
|
}
|
|
|
|
// Create Avro message (simulate what a Kafka producer would send)
|
|
avroSchema := getUserAvroSchema()
|
|
codec, err := goavro.NewCodec(avroSchema)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create Avro codec: %v", err)
|
|
}
|
|
|
|
avroBinary, err := codec.BinaryFromNative(nil, userData)
|
|
if err != nil {
|
|
t.Fatalf("Failed to encode Avro data: %v", err)
|
|
}
|
|
|
|
// Create Confluent envelope (what Kafka Gateway receives)
|
|
confluentMsg := CreateConfluentEnvelope(FormatAvro, 1, nil, avroBinary)
|
|
|
|
// Decode message (Produce path processing)
|
|
decodedMsg, err := manager.DecodeMessage(confluentMsg)
|
|
if err != nil {
|
|
t.Fatalf("Failed to decode message: %v", err)
|
|
}
|
|
|
|
// Verify decoded data
|
|
if decodedMsg.SchemaID != 1 {
|
|
t.Errorf("Expected schema ID 1, got %d", decodedMsg.SchemaID)
|
|
}
|
|
|
|
if decodedMsg.SchemaFormat != FormatAvro {
|
|
t.Errorf("Expected Avro format, got %v", decodedMsg.SchemaFormat)
|
|
}
|
|
|
|
// Verify field values
|
|
fields := decodedMsg.RecordValue.Fields
|
|
if fields["id"].GetInt32Value() != 12345 {
|
|
t.Errorf("Expected id=12345, got %v", fields["id"].GetInt32Value())
|
|
}
|
|
|
|
if fields["name"].GetStringValue() != "Alice Johnson" {
|
|
t.Errorf("Expected name='Alice Johnson', got %v", fields["name"].GetStringValue())
|
|
}
|
|
|
|
t.Logf("Successfully processed producer message with %d fields", len(fields))
|
|
})
|
|
|
|
// Test 2: Consumer workflow - reconstruct original message
|
|
t.Run("Consumer_Workflow", func(t *testing.T) {
|
|
// Create test RecordValue (simulate what's stored in SeaweedMQ)
|
|
testData := map[string]interface{}{
|
|
"id": int32(67890),
|
|
"name": "Bob Smith",
|
|
"email": map[string]interface{}{"string": "bob@example.com"},
|
|
"age": map[string]interface{}{"int": int32(35)}, // Avro union
|
|
}
|
|
recordValue := MapToRecordValue(testData)
|
|
|
|
// Reconstruct message (Fetch path processing)
|
|
reconstructedMsg, err := manager.EncodeMessage(recordValue, 1, FormatAvro)
|
|
if err != nil {
|
|
t.Fatalf("Failed to reconstruct message: %v", err)
|
|
}
|
|
|
|
// Verify reconstructed message can be parsed
|
|
envelope, ok := ParseConfluentEnvelope(reconstructedMsg)
|
|
if !ok {
|
|
t.Fatal("Failed to parse reconstructed envelope")
|
|
}
|
|
|
|
if envelope.SchemaID != 1 {
|
|
t.Errorf("Expected schema ID 1, got %d", envelope.SchemaID)
|
|
}
|
|
|
|
// Verify the payload can be decoded by Avro
|
|
avroSchema := getUserAvroSchema()
|
|
codec, err := goavro.NewCodec(avroSchema)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create Avro codec: %v", err)
|
|
}
|
|
|
|
decodedData, _, err := codec.NativeFromBinary(envelope.Payload)
|
|
if err != nil {
|
|
t.Fatalf("Failed to decode reconstructed Avro data: %v", err)
|
|
}
|
|
|
|
// Verify data integrity
|
|
decodedMap := decodedData.(map[string]interface{})
|
|
if decodedMap["id"] != int32(67890) {
|
|
t.Errorf("Expected id=67890, got %v", decodedMap["id"])
|
|
}
|
|
|
|
if decodedMap["name"] != "Bob Smith" {
|
|
t.Errorf("Expected name='Bob Smith', got %v", decodedMap["name"])
|
|
}
|
|
|
|
t.Logf("Successfully reconstructed consumer message: %d bytes", len(reconstructedMsg))
|
|
})
|
|
|
|
// Test 3: Round-trip integrity
|
|
t.Run("Round_Trip_Integrity", func(t *testing.T) {
|
|
originalData := map[string]interface{}{
|
|
"id": int32(99999),
|
|
"name": "Charlie Brown",
|
|
"email": map[string]interface{}{"string": "charlie@example.com"},
|
|
"age": map[string]interface{}{"int": int32(42)}, // Avro union
|
|
"preferences": map[string]interface{}{
|
|
"Preferences": map[string]interface{}{ // Avro union with record type
|
|
"notifications": true,
|
|
"theme": "dark",
|
|
},
|
|
},
|
|
}
|
|
|
|
// Encode -> Decode -> Encode -> Decode
|
|
avroSchema := getUserAvroSchema()
|
|
codec, _ := goavro.NewCodec(avroSchema)
|
|
|
|
// Step 1: Original -> Confluent
|
|
avroBinary, _ := codec.BinaryFromNative(nil, originalData)
|
|
confluentMsg := CreateConfluentEnvelope(FormatAvro, 1, nil, avroBinary)
|
|
|
|
// Step 2: Confluent -> RecordValue
|
|
decodedMsg, _ := manager.DecodeMessage(confluentMsg)
|
|
|
|
// Step 3: RecordValue -> Confluent
|
|
reconstructedMsg, encodeErr := manager.EncodeMessage(decodedMsg.RecordValue, 1, FormatAvro)
|
|
if encodeErr != nil {
|
|
t.Fatalf("Failed to encode message: %v", encodeErr)
|
|
}
|
|
|
|
// Verify the reconstructed message is valid
|
|
if len(reconstructedMsg) == 0 {
|
|
t.Fatal("Reconstructed message is empty")
|
|
}
|
|
|
|
// Step 4: Confluent -> Verify
|
|
finalDecodedMsg, err := manager.DecodeMessage(reconstructedMsg)
|
|
if err != nil {
|
|
// Debug: Check if the reconstructed message is properly formatted
|
|
envelope, ok := ParseConfluentEnvelope(reconstructedMsg)
|
|
if !ok {
|
|
t.Fatalf("Round-trip failed: reconstructed message is not a valid Confluent envelope")
|
|
}
|
|
t.Logf("Debug: Envelope SchemaID=%d, Format=%v, PayloadLen=%d",
|
|
envelope.SchemaID, envelope.Format, len(envelope.Payload))
|
|
t.Fatalf("Round-trip failed: %v", err)
|
|
}
|
|
|
|
// Verify data integrity through complete round-trip
|
|
finalFields := finalDecodedMsg.RecordValue.Fields
|
|
if finalFields["id"].GetInt32Value() != 99999 {
|
|
t.Error("Round-trip failed for id field")
|
|
}
|
|
|
|
if finalFields["name"].GetStringValue() != "Charlie Brown" {
|
|
t.Error("Round-trip failed for name field")
|
|
}
|
|
|
|
t.Log("Round-trip integrity test passed")
|
|
})
|
|
}
|
|
|
|
// TestFullIntegration_MultiFormatSupport tests all schema formats together
|
|
func TestFullIntegration_MultiFormatSupport(t *testing.T) {
|
|
server := createMockSchemaRegistry(t)
|
|
defer server.Close()
|
|
|
|
config := ManagerConfig{
|
|
RegistryURL: server.URL,
|
|
ValidationMode: ValidationPermissive,
|
|
}
|
|
|
|
manager, err := NewManager(config)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create manager: %v", err)
|
|
}
|
|
|
|
testCases := []struct {
|
|
name string
|
|
format Format
|
|
schemaID uint32
|
|
testData interface{}
|
|
}{
|
|
{
|
|
name: "Avro_Format",
|
|
format: FormatAvro,
|
|
schemaID: 1,
|
|
testData: map[string]interface{}{
|
|
"id": int32(123),
|
|
"name": "Avro User",
|
|
},
|
|
},
|
|
{
|
|
name: "JSON_Schema_Format",
|
|
format: FormatJSONSchema,
|
|
schemaID: 3,
|
|
testData: map[string]interface{}{
|
|
"id": float64(456), // JSON numbers are float64
|
|
"name": "JSON User",
|
|
"active": true,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
// Create RecordValue from test data
|
|
recordValue := MapToRecordValue(tc.testData.(map[string]interface{}))
|
|
|
|
// Test encoding
|
|
encoded, err := manager.EncodeMessage(recordValue, tc.schemaID, tc.format)
|
|
if err != nil {
|
|
if tc.format == FormatProtobuf {
|
|
// Protobuf encoding may fail due to incomplete implementation
|
|
t.Skipf("Protobuf encoding not fully implemented: %v", err)
|
|
} else {
|
|
t.Fatalf("Failed to encode %s message: %v", tc.name, err)
|
|
}
|
|
}
|
|
|
|
// Test decoding
|
|
decoded, err := manager.DecodeMessage(encoded)
|
|
if err != nil {
|
|
t.Fatalf("Failed to decode %s message: %v", tc.name, err)
|
|
}
|
|
|
|
// Verify format
|
|
if decoded.SchemaFormat != tc.format {
|
|
t.Errorf("Expected format %v, got %v", tc.format, decoded.SchemaFormat)
|
|
}
|
|
|
|
// Verify schema ID
|
|
if decoded.SchemaID != tc.schemaID {
|
|
t.Errorf("Expected schema ID %d, got %d", tc.schemaID, decoded.SchemaID)
|
|
}
|
|
|
|
t.Logf("Successfully processed %s format", tc.name)
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestIntegration_CachePerformance tests caching behavior under load
|
|
func TestIntegration_CachePerformance(t *testing.T) {
|
|
server := createMockSchemaRegistry(t)
|
|
defer server.Close()
|
|
|
|
config := ManagerConfig{
|
|
RegistryURL: server.URL,
|
|
ValidationMode: ValidationPermissive,
|
|
}
|
|
|
|
manager, err := NewManager(config)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create manager: %v", err)
|
|
}
|
|
|
|
// Create test message
|
|
testData := map[string]interface{}{
|
|
"id": int32(1),
|
|
"name": "Cache Test",
|
|
}
|
|
|
|
avroSchema := getUserAvroSchema()
|
|
codec, _ := goavro.NewCodec(avroSchema)
|
|
avroBinary, _ := codec.BinaryFromNative(nil, testData)
|
|
testMsg := CreateConfluentEnvelope(FormatAvro, 1, nil, avroBinary)
|
|
|
|
// First decode (should hit registry)
|
|
start := time.Now()
|
|
_, err = manager.DecodeMessage(testMsg)
|
|
if err != nil {
|
|
t.Fatalf("First decode failed: %v", err)
|
|
}
|
|
firstDuration := time.Since(start)
|
|
|
|
// Subsequent decodes (should hit cache)
|
|
start = time.Now()
|
|
for i := 0; i < 100; i++ {
|
|
_, err = manager.DecodeMessage(testMsg)
|
|
if err != nil {
|
|
t.Fatalf("Cached decode failed: %v", err)
|
|
}
|
|
}
|
|
cachedDuration := time.Since(start)
|
|
|
|
// Verify cache performance improvement
|
|
avgCachedTime := cachedDuration / 100
|
|
if avgCachedTime >= firstDuration {
|
|
t.Logf("Warning: Cache may not be effective. First: %v, Avg Cached: %v",
|
|
firstDuration, avgCachedTime)
|
|
}
|
|
|
|
// Check cache stats
|
|
decoders, schemas, subjects := manager.GetCacheStats()
|
|
if decoders == 0 || schemas == 0 {
|
|
t.Error("Expected non-zero cache stats")
|
|
}
|
|
|
|
t.Logf("Cache performance: First decode: %v, Average cached: %v",
|
|
firstDuration, avgCachedTime)
|
|
t.Logf("Cache stats: %d decoders, %d schemas, %d subjects",
|
|
decoders, schemas, subjects)
|
|
}
|
|
|
|
// TestIntegration_ErrorHandling tests error scenarios
|
|
func TestIntegration_ErrorHandling(t *testing.T) {
|
|
server := createMockSchemaRegistry(t)
|
|
defer server.Close()
|
|
|
|
config := ManagerConfig{
|
|
RegistryURL: server.URL,
|
|
ValidationMode: ValidationStrict,
|
|
}
|
|
|
|
manager, err := NewManager(config)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create manager: %v", err)
|
|
}
|
|
|
|
testCases := []struct {
|
|
name string
|
|
message []byte
|
|
expectError bool
|
|
errorType string
|
|
}{
|
|
{
|
|
name: "Non_Schematized_Message",
|
|
message: []byte("plain text message"),
|
|
expectError: true,
|
|
errorType: "not schematized",
|
|
},
|
|
{
|
|
name: "Invalid_Schema_ID",
|
|
message: CreateConfluentEnvelope(FormatAvro, 999, nil, []byte("payload")),
|
|
expectError: true,
|
|
errorType: "schema not found",
|
|
},
|
|
{
|
|
name: "Empty_Payload",
|
|
message: CreateConfluentEnvelope(FormatAvro, 1, nil, []byte{}),
|
|
expectError: true,
|
|
errorType: "empty payload",
|
|
},
|
|
{
|
|
name: "Corrupted_Avro_Data",
|
|
message: CreateConfluentEnvelope(FormatAvro, 1, nil, []byte("invalid avro")),
|
|
expectError: true,
|
|
errorType: "decode failed",
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
_, err := manager.DecodeMessage(tc.message)
|
|
|
|
if (err != nil) != tc.expectError {
|
|
t.Errorf("Expected error: %v, got error: %v", tc.expectError, err != nil)
|
|
}
|
|
|
|
if tc.expectError && err != nil {
|
|
t.Logf("Expected error occurred: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestIntegration_SchemaEvolution tests schema evolution scenarios
|
|
func TestIntegration_SchemaEvolution(t *testing.T) {
|
|
server := createMockSchemaRegistryWithEvolution(t)
|
|
defer server.Close()
|
|
|
|
config := ManagerConfig{
|
|
RegistryURL: server.URL,
|
|
ValidationMode: ValidationPermissive,
|
|
}
|
|
|
|
manager, err := NewManager(config)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create manager: %v", err)
|
|
}
|
|
|
|
// Test decoding messages with different schema versions
|
|
t.Run("Schema_V1_Message", func(t *testing.T) {
|
|
// Create message with schema v1 (basic user)
|
|
userData := map[string]interface{}{
|
|
"id": int32(1),
|
|
"name": "User V1",
|
|
}
|
|
|
|
avroSchema := getUserAvroSchemaV1()
|
|
codec, _ := goavro.NewCodec(avroSchema)
|
|
avroBinary, _ := codec.BinaryFromNative(nil, userData)
|
|
msg := CreateConfluentEnvelope(FormatAvro, 1, nil, avroBinary)
|
|
|
|
decoded, err := manager.DecodeMessage(msg)
|
|
if err != nil {
|
|
t.Fatalf("Failed to decode v1 message: %v", err)
|
|
}
|
|
|
|
if decoded.Version != 1 {
|
|
t.Errorf("Expected version 1, got %d", decoded.Version)
|
|
}
|
|
})
|
|
|
|
t.Run("Schema_V2_Message", func(t *testing.T) {
|
|
// Create message with schema v2 (user with email)
|
|
userData := map[string]interface{}{
|
|
"id": int32(2),
|
|
"name": "User V2",
|
|
"email": map[string]interface{}{"string": "user@example.com"},
|
|
}
|
|
|
|
avroSchema := getUserAvroSchemaV2()
|
|
codec, _ := goavro.NewCodec(avroSchema)
|
|
avroBinary, _ := codec.BinaryFromNative(nil, userData)
|
|
msg := CreateConfluentEnvelope(FormatAvro, 2, nil, avroBinary)
|
|
|
|
decoded, err := manager.DecodeMessage(msg)
|
|
if err != nil {
|
|
t.Fatalf("Failed to decode v2 message: %v", err)
|
|
}
|
|
|
|
if decoded.Version != 2 {
|
|
t.Errorf("Expected version 2, got %d", decoded.Version)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Helper functions for creating mock schema registries
|
|
|
|
func createMockSchemaRegistry(t *testing.T) *httptest.Server {
|
|
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case "/subjects":
|
|
// List subjects
|
|
subjects := []string{"user-value", "product-value", "order-value"}
|
|
json.NewEncoder(w).Encode(subjects)
|
|
|
|
case "/schemas/ids/1":
|
|
// Avro user schema
|
|
response := map[string]interface{}{
|
|
"schema": getUserAvroSchema(),
|
|
"subject": "user-value",
|
|
"version": 1,
|
|
}
|
|
json.NewEncoder(w).Encode(response)
|
|
|
|
case "/schemas/ids/2":
|
|
// Protobuf schema (simplified)
|
|
response := map[string]interface{}{
|
|
"schema": "syntax = \"proto3\"; message User { int32 id = 1; string name = 2; }",
|
|
"subject": "user-value",
|
|
"version": 2,
|
|
}
|
|
json.NewEncoder(w).Encode(response)
|
|
|
|
case "/schemas/ids/3":
|
|
// JSON Schema
|
|
response := map[string]interface{}{
|
|
"schema": getUserJSONSchema(),
|
|
"subject": "user-value",
|
|
"version": 3,
|
|
}
|
|
json.NewEncoder(w).Encode(response)
|
|
|
|
default:
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}
|
|
}))
|
|
}
|
|
|
|
func createMockSchemaRegistryWithEvolution(t *testing.T) *httptest.Server {
|
|
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case "/schemas/ids/1":
|
|
// Schema v1
|
|
response := map[string]interface{}{
|
|
"schema": getUserAvroSchemaV1(),
|
|
"subject": "user-value",
|
|
"version": 1,
|
|
}
|
|
json.NewEncoder(w).Encode(response)
|
|
|
|
case "/schemas/ids/2":
|
|
// Schema v2 (evolved)
|
|
response := map[string]interface{}{
|
|
"schema": getUserAvroSchemaV2(),
|
|
"subject": "user-value",
|
|
"version": 2,
|
|
}
|
|
json.NewEncoder(w).Encode(response)
|
|
|
|
default:
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}
|
|
}))
|
|
}
|
|
|
|
// Schema definitions for testing
|
|
|
|
func getUserAvroSchema() string {
|
|
return `{
|
|
"type": "record",
|
|
"name": "User",
|
|
"fields": [
|
|
{"name": "id", "type": "int"},
|
|
{"name": "name", "type": "string"},
|
|
{"name": "email", "type": ["null", "string"], "default": null},
|
|
{"name": "age", "type": ["null", "int"], "default": null},
|
|
{"name": "preferences", "type": ["null", {
|
|
"type": "record",
|
|
"name": "Preferences",
|
|
"fields": [
|
|
{"name": "notifications", "type": "boolean", "default": true},
|
|
{"name": "theme", "type": "string", "default": "light"}
|
|
]
|
|
}], "default": null}
|
|
]
|
|
}`
|
|
}
|
|
|
|
func getUserAvroSchemaV1() string {
|
|
return `{
|
|
"type": "record",
|
|
"name": "User",
|
|
"fields": [
|
|
{"name": "id", "type": "int"},
|
|
{"name": "name", "type": "string"}
|
|
]
|
|
}`
|
|
}
|
|
|
|
func getUserAvroSchemaV2() string {
|
|
return `{
|
|
"type": "record",
|
|
"name": "User",
|
|
"fields": [
|
|
{"name": "id", "type": "int"},
|
|
{"name": "name", "type": "string"},
|
|
{"name": "email", "type": ["null", "string"], "default": null}
|
|
]
|
|
}`
|
|
}
|
|
|
|
func getUserJSONSchema() string {
|
|
return `{
|
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
"type": "object",
|
|
"properties": {
|
|
"id": {"type": "integer"},
|
|
"name": {"type": "string"},
|
|
"active": {"type": "boolean"}
|
|
},
|
|
"required": ["id", "name"]
|
|
}`
|
|
}
|
|
|
|
// Benchmark tests for integration scenarios
|
|
|
|
func BenchmarkIntegration_AvroDecoding(b *testing.B) {
|
|
server := createMockSchemaRegistry(nil)
|
|
defer server.Close()
|
|
|
|
config := ManagerConfig{RegistryURL: server.URL}
|
|
manager, _ := NewManager(config)
|
|
|
|
// Create test message
|
|
testData := map[string]interface{}{
|
|
"id": int32(1),
|
|
"name": "Benchmark User",
|
|
}
|
|
|
|
avroSchema := getUserAvroSchema()
|
|
codec, _ := goavro.NewCodec(avroSchema)
|
|
avroBinary, _ := codec.BinaryFromNative(nil, testData)
|
|
testMsg := CreateConfluentEnvelope(FormatAvro, 1, nil, avroBinary)
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_, _ = manager.DecodeMessage(testMsg)
|
|
}
|
|
}
|
|
|
|
func BenchmarkIntegration_JSONSchemaDecoding(b *testing.B) {
|
|
server := createMockSchemaRegistry(nil)
|
|
defer server.Close()
|
|
|
|
config := ManagerConfig{RegistryURL: server.URL}
|
|
manager, _ := NewManager(config)
|
|
|
|
// Create test message
|
|
jsonData := []byte(`{"id": 1, "name": "Benchmark User", "active": true}`)
|
|
testMsg := CreateConfluentEnvelope(FormatJSONSchema, 3, nil, jsonData)
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_, _ = manager.DecodeMessage(testMsg)
|
|
}
|
|
}
|