You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
1.9 KiB
89 lines
1.9 KiB
package simulation |
|
|
|
import ( |
|
"os" |
|
"path/filepath" |
|
"testing" |
|
|
|
"encoding/json" |
|
"systemdesigngame/internal/design" |
|
) |
|
|
|
func TestNewEngineFromDesign(t *testing.T) { |
|
designInput := &design.Design{ |
|
Nodes: []design.Node{ |
|
{ |
|
ID: "web1", |
|
Type: "webserver", |
|
Props: map[string]interface{}{ |
|
"cpu": float64(2), |
|
"ramGb": float64(4), |
|
"rpsCapacity": float64(100), |
|
"monthlyCostUsd": float64(20), |
|
}, |
|
}, |
|
{ |
|
ID: "cache1", |
|
Type: "cache", |
|
Props: map[string]interface{}{ |
|
"label": "L1 Cache", |
|
"cacheTTL": float64(60), |
|
"maxEntries": float64(1000), |
|
"evictionPolicy": "LRU", |
|
}, |
|
}, |
|
}, |
|
Connections: []design.Connection{ |
|
{ |
|
Source: "web1", |
|
Target: "cache1", |
|
}, |
|
}, |
|
} |
|
|
|
engine := NewEngineFromDesign(*designInput, 10, 100) |
|
|
|
if len(engine.Nodes) != 2 { |
|
t.Fatalf("expected 2 nodes, got %d", len(engine.Nodes)) |
|
} |
|
|
|
if len(engine.Nodes["web1"].GetTargets()) != 1 { |
|
t.Fatalf("expected web1 to have 1 target, got %d", len(engine.Nodes["web1"].GetTargets())) |
|
} |
|
|
|
if engine.Nodes["web1"].GetTargets()[0] != "cache1" { |
|
t.Fatalf("expected web1 target to be cache1") |
|
} |
|
} |
|
|
|
func TestComplexSimulationRun(t *testing.T) { |
|
filePath := filepath.Join("testdata", "complex_design.json") |
|
data, err := os.ReadFile(filePath) |
|
if err != nil { |
|
t.Fatalf("Failed to read JSON file: %v", err) |
|
} |
|
|
|
var d design.Design |
|
if err := json.Unmarshal([]byte(data), &d); err != nil { |
|
t.Fatalf("Failed to unmarshal JSON: %v", err) |
|
} |
|
|
|
engine := NewEngineFromDesign(d, 10, 100) |
|
if engine == nil { |
|
t.Fatal("Engine should not be nil") |
|
} |
|
|
|
engine.Run() |
|
|
|
if len(engine.Timeline) == 0 { |
|
t.Fatal("Expected timeline snapshots after Run, got none") |
|
} |
|
|
|
// Optional: check that some nodes received or emitted requests |
|
for id, node := range engine.Nodes { |
|
if len(node.Emit()) > 0 { |
|
t.Logf("Node %s has activity", id) |
|
} |
|
} |
|
} |
|
|
|
|