Files
osmedeus/internal/database/write_coordinator_test.go
T
j3ssie f5840272c5 feat: add run cancellation, event enhancements, and performance optimizations
Major features:
- Add run registry for tracking active runs with PID management
- Add API-based run cancellation with process termination
- Add event trigger input vars syntax for multi-variable extraction
- Add filter_functions with utility function support in triggers
- Add event envelope injection for full event context in workflows
- Add write coordinator for batched database operations

API improvements:
- Add logout endpoint and diffs endpoints for assets/vulnerabilities
- Add step-results listing endpoint
- Update schedule model with target, workspace, params fields
- Change run_id to run_uuid across API responses

Performance:
- Add compiled JS program caching for 60-80% faster loop conditions
- Add parallel shard rendering for 20-40% faster workflow startup
- Add memory-mapped I/O for large file line counting
- Add efficient output buffer combining in runners
- Add mtime-based cache invalidation for workflow loader

Other changes:
- Rename trigger field from trigger to triggers in workflow YAML
- Disable pongo2 HTML autoescape for shell command templates
- Update JWT expiration default to 1440 minutes (1 day)
- Change CORS default to reflect-origin for credentials support
- Add source_type field to events (run, eval, api)
- Skip copying core Unix tools to external-binaries
2026-01-24 01:11:33 +08:00

129 lines
3.3 KiB
Go

package database
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWriteCoordinator_Basic(t *testing.T) {
cfg := &WriteCoordinatorConfig{
FlushThreshold: 5,
FlushInterval: 1 * time.Second,
}
wc := NewWriteCoordinator(1, "test-uuid", cfg)
require.NotNil(t, wc)
// Initially empty
assert.Equal(t, 0, wc.Len())
assert.Equal(t, 0, wc.PendingProgress())
// Add step results
now := time.Now()
wc.AddStepResult("step1", "bash", "success", "echo hello", "hello", "", nil, 100, &now, &now)
assert.Equal(t, 1, wc.Len())
// Add progress
wc.IncrementProgress(1)
assert.Equal(t, 1, wc.PendingProgress())
// Stop the coordinator
wc.Stop()
}
func TestWriteCoordinator_AutoFlush(t *testing.T) {
cfg := &WriteCoordinatorConfig{
FlushThreshold: 3, // Small threshold for testing
FlushInterval: 10 * time.Second,
}
wc := NewWriteCoordinator(1, "test-uuid", cfg)
defer wc.Stop()
now := time.Now()
// Add 2 step results - should not auto-flush
wc.AddStepResult("step1", "bash", "success", "cmd1", "out1", "", nil, 100, &now, &now)
wc.AddStepResult("step2", "bash", "success", "cmd2", "out2", "", nil, 100, &now, &now)
assert.Equal(t, 2, wc.Len())
// Add 3rd step result - should trigger auto-flush (threshold reached)
// Note: Without a DB, the flush clears the buffer but doesn't persist
wc.AddStepResult("step3", "bash", "success", "cmd3", "out3", "", nil, 100, &now, &now)
// After auto-flush, buffer should be cleared
assert.Equal(t, 0, wc.Len())
}
func TestWriteCoordinator_ManualFlush(t *testing.T) {
cfg := &WriteCoordinatorConfig{
FlushThreshold: 100, // High threshold to prevent auto-flush
FlushInterval: 10 * time.Second,
}
wc := NewWriteCoordinator(1, "test-uuid", cfg)
defer wc.Stop()
now := time.Now()
// Add step results
wc.AddStepResult("step1", "bash", "success", "cmd1", "out1", "", nil, 100, &now, &now)
wc.IncrementProgress(1)
assert.Equal(t, 1, wc.Len())
assert.Equal(t, 1, wc.PendingProgress())
// Manual flush
err := wc.Flush(context.Background())
require.NoError(t, err)
// After flush, buffer should be cleared
assert.Equal(t, 0, wc.Len())
assert.Equal(t, 0, wc.PendingProgress())
}
func TestWriteCoordinator_FlushAll(t *testing.T) {
cfg := &WriteCoordinatorConfig{
FlushThreshold: 100,
FlushInterval: 10 * time.Second,
}
wc := NewWriteCoordinator(1, "test-uuid", cfg)
now := time.Now()
wc.AddStepResult("step1", "bash", "success", "cmd1", "out1", "", nil, 100, &now, &now)
wc.IncrementProgress(1)
// FlushAll should stop the coordinator and flush
err := wc.FlushAll(context.Background())
require.NoError(t, err)
// After FlushAll, buffer should be cleared
assert.Equal(t, 0, wc.Len())
assert.Equal(t, 0, wc.PendingProgress())
// Subsequent FlushAll should be a no-op
err = wc.FlushAll(context.Background())
require.NoError(t, err)
}
func TestWriteCoordinator_EmptyFlush(t *testing.T) {
wc := NewWriteCoordinator(1, "test-uuid", nil)
defer wc.Stop()
// Flushing empty coordinator should be a no-op
err := wc.Flush(context.Background())
require.NoError(t, err)
}
func TestWriteCoordinator_DefaultConfig(t *testing.T) {
cfg := DefaultWriteCoordinatorConfig()
require.NotNil(t, cfg)
assert.Equal(t, 10, cfg.FlushThreshold)
assert.Equal(t, 5*time.Second, cfg.FlushInterval)
}