Skip to content

Commit 0e7e906

Browse files
committed
unit tests
Signed-off-by: MorningTZH <morningtzh@yeah.net>
1 parent 25ac879 commit 0e7e906

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package nodemanager_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/e2b-dev/infra/packages/api/internal/api"
9+
"github.com/e2b-dev/infra/packages/api/internal/orchestrator/nodemanager"
10+
)
11+
12+
func TestNode_OptimisticAdd(t *testing.T) {
13+
t.Parallel()
14+
15+
// Initialize an idle node
16+
node := nodemanager.NewTestNode("test-node", api.NodeStatusReady, 0, 4)
17+
initialMetrics := node.Metrics()
18+
19+
// Simulate the resources to be allocated
20+
res := nodemanager.SandboxResources{
21+
CPUs: 2,
22+
MiBMemory: 1024, // 1GB
23+
}
24+
25+
// Perform optimistic addition
26+
node.OptimisticAdd(res)
27+
newMetrics := node.Metrics()
28+
29+
// Verify that CPU and memory are increased as expected
30+
assert.Equal(t, initialMetrics.CpuAllocated+uint32(res.CPUs), newMetrics.CpuAllocated)
31+
assert.Equal(t, initialMetrics.MemoryAllocatedBytes+uint64(res.MiBMemory)*1024*1024, newMetrics.MemoryAllocatedBytes)
32+
}

packages/api/internal/orchestrator/placement/placement_best_of_K_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,36 @@ func TestBestOfK_Score_PreferBiggerNode(t *testing.T) {
6565
assert.Greater(t, score, score2)
6666
}
6767

68+
func TestBestOfK_Score_WithPendingResources(t *testing.T) {
69+
t.Parallel()
70+
config := DefaultBestOfKConfig()
71+
algo := NewBestOfK(config).(*BestOfK)
72+
73+
// Create two nodes with identical base loads
74+
nodeNormal := nodemanager.NewTestNode("node-normal", api.NodeStatusReady, 0, 4)
75+
nodeWithPending := nodemanager.NewTestNode("node-pending", api.NodeStatusReady, 0, 4)
76+
77+
// Inject InProgress resources into nodeWithPending using StartPlacing
78+
// This simulates a Sandbox that is currently being placed but hasn't fully started
79+
pendingRes := nodemanager.SandboxResources{
80+
CPUs: 2,
81+
MiBMemory: 1024,
82+
}
83+
nodeWithPending.PlacementMetrics.StartPlacing("pending-sbx-1", pendingRes)
84+
85+
reqResources := nodemanager.SandboxResources{
86+
CPUs: 1,
87+
MiBMemory: 512,
88+
}
89+
90+
scoreNormal := algo.Score(nodeNormal, reqResources, config)
91+
scorePending := algo.Score(nodeWithPending, reqResources, config)
92+
93+
// A node with pending resources has a higher 'reserved' CPU count,
94+
// so its calculated Score should be greater (meaning worse/lower priority)
95+
assert.Greater(t, scorePending, scoreNormal, "Node with pending resources should receive a higher (worse) score")
96+
}
97+
6898
func TestBestOfK_CanFit(t *testing.T) {
6999
t.Parallel()
70100
config := DefaultBestOfKConfig()

packages/api/internal/orchestrator/placement/placement_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,37 @@ func TestPlaceSandbox_ResourceExhausted(t *testing.T) {
213213
// Verify node1 was NOT excluded (ResourceExhausted nodes should be retried)
214214
algorithm.AssertNumberOfCalls(t, "chooseNode", 2)
215215
}
216+
217+
func TestPlaceSandbox_TriggersOptimisticUpdate(t *testing.T) {
218+
t.Parallel()
219+
ctx := t.Context()
220+
221+
// Create a node and record the initial allocated CPU
222+
node1 := nodemanager.NewTestNode("node1", api.NodeStatusReady, 0, 4)
223+
initialCpuAllocated := node1.Metrics().CpuAllocated
224+
225+
nodes := []*nodemanager.Node{node1}
226+
227+
// Mock algorithm directly returns node1
228+
algorithm := &mockAlgorithm{}
229+
algorithm.On("chooseNode", mock.Anything, nodes, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
230+
Return(node1, nil)
231+
232+
// Request 2 vCPUs
233+
sbxRequest := &orchestrator.SandboxCreateRequest{
234+
Sandbox: &orchestrator.SandboxConfig{
235+
SandboxId: "test-optimistic-sandbox",
236+
Vcpu: 2,
237+
RamMb: 1024,
238+
},
239+
}
240+
241+
resultNode, err := PlaceSandbox(ctx, algorithm, nodes, nil, sbxRequest, machineinfo.MachineInfo{}, false, nil)
242+
243+
require.NoError(t, err)
244+
assert.NotNil(t, resultNode)
245+
246+
// Verify: After successful placement, the node's CpuAllocated should be increased by 2 from the base
247+
updatedCpuAllocated := resultNode.Metrics().CpuAllocated
248+
assert.Equal(t, initialCpuAllocated+2, updatedCpuAllocated, "Node metrics should be optimistically updated after placement")
249+
}

0 commit comments

Comments
 (0)