go-libp2p-resource-manager/scope_test.go

1201 lines
42 KiB
Go
Raw Permalink Normal View History

2021-12-27 21:51:25 +08:00
package rcmgr
import (
"testing"
"github.com/libp2p/go-libp2p-core/network"
)
2021-12-28 22:36:08 +08:00
func checkResources(t *testing.T, rc *resources, st network.ScopeStat) {
2021-12-27 21:51:25 +08:00
t.Helper()
if rc.nconnsIn != st.NumConnsInbound {
t.Fatalf("expected %d inbound conns, got %d", st.NumConnsInbound, rc.nconnsIn)
}
if rc.nconnsOut != st.NumConnsOutbound {
t.Fatalf("expected %d outbound conns, got %d", st.NumConnsOutbound, rc.nconnsOut)
}
if rc.nstreamsIn != st.NumStreamsInbound {
t.Fatalf("expected %d inbound streams, got %d", st.NumStreamsInbound, rc.nstreamsIn)
}
if rc.nstreamsOut != st.NumStreamsOutbound {
t.Fatalf("expected %d outbound streams, got %d", st.NumStreamsOutbound, rc.nstreamsOut)
}
if rc.nfd != st.NumFD {
t.Fatalf("expected %d file descriptors, got %d", st.NumFD, rc.nfd)
}
if rc.memory != st.Memory {
t.Fatalf("expected %d reserved bytes of memory, got %d", st.Memory, rc.memory)
}
}
func TestResources(t *testing.T) {
2022-06-10 22:01:03 +08:00
rc := resources{limit: &BaseLimit{
Memory: 4096,
StreamsInbound: 1,
StreamsOutbound: 1,
Streams: 1,
ConnsInbound: 1,
ConnsOutbound: 1,
Conns: 1,
FD: 1,
2021-12-30 01:39:09 +08:00
}}
2021-12-27 21:51:25 +08:00
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{})
2021-12-27 21:51:25 +08:00
2022-01-06 16:35:06 +08:00
// test checkMemory
2022-01-06 16:27:23 +08:00
if err := rc.checkMemory(1024, network.ReservationPriorityAlways); err != nil {
2021-12-27 21:51:25 +08:00
t.Fatal(err)
}
2022-01-05 16:06:11 +08:00
2022-01-06 16:27:23 +08:00
if err := rc.checkMemory(2048, network.ReservationPriorityAlways); err != nil {
2021-12-27 21:51:25 +08:00
t.Fatal(err)
}
2022-01-05 16:06:11 +08:00
2022-01-06 16:27:23 +08:00
if err := rc.checkMemory(3072, network.ReservationPriorityAlways); err != nil {
2022-01-05 16:06:11 +08:00
t.Fatal(err)
}
2022-01-06 16:27:23 +08:00
if err := rc.checkMemory(4096, network.ReservationPriorityAlways); err != nil {
2022-01-05 16:06:11 +08:00
t.Fatal(err)
}
2022-01-06 16:27:23 +08:00
if err := rc.checkMemory(8192, network.ReservationPriorityAlways); err == nil {
2021-12-27 21:51:25 +08:00
t.Fatal("expected memory check to fail")
}
2022-01-06 16:35:06 +08:00
if err := rc.checkMemory(1024, network.ReservationPriorityLow); err != nil {
t.Fatal(err)
}
if err := rc.checkMemory(2048, network.ReservationPriorityLow); err == nil {
t.Fatal("expected memory check to fail")
}
if err := rc.checkMemory(2048, network.ReservationPriorityMedium); err != nil {
t.Fatal(err)
}
if err := rc.checkMemory(3072, network.ReservationPriorityMedium); err == nil {
t.Fatal("expected memory check to fail")
}
if err := rc.checkMemory(3072, network.ReservationPriorityHigh); err != nil {
t.Fatal(err)
}
if err := rc.checkMemory(3584, network.ReservationPriorityHigh); err == nil {
t.Fatal("expected memory check to fail")
}
// test reserveMemory
2022-01-06 16:27:23 +08:00
if err := rc.reserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2021-12-27 21:51:25 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{Memory: 1024})
2021-12-27 21:51:25 +08:00
2022-01-06 16:27:23 +08:00
if err := rc.reserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2021-12-27 21:51:25 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{Memory: 2048})
2021-12-27 21:51:25 +08:00
2022-01-06 16:27:23 +08:00
if err := rc.reserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2022-01-05 16:06:11 +08:00
t.Fatal(err)
}
checkResources(t, &rc, network.ScopeStat{Memory: 3072})
2022-01-06 16:27:23 +08:00
if err := rc.reserveMemory(512, network.ReservationPriorityAlways); err != nil {
2022-01-05 16:06:11 +08:00
t.Fatal(err)
}
checkResources(t, &rc, network.ScopeStat{Memory: 3584})
2022-01-06 16:27:23 +08:00
if err := rc.reserveMemory(4096, network.ReservationPriorityAlways); err == nil {
2021-12-27 21:51:25 +08:00
t.Fatal("expected memory reservation to fail")
}
2022-01-05 16:06:11 +08:00
checkResources(t, &rc, network.ScopeStat{Memory: 3584})
2021-12-27 21:51:25 +08:00
2022-01-05 16:06:11 +08:00
rc.releaseMemory(2560)
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{Memory: 1024})
2021-12-27 21:51:25 +08:00
2022-01-06 16:27:23 +08:00
if err := rc.reserveMemory(2048, network.ReservationPriorityAlways); err != nil {
2021-12-27 21:51:25 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{Memory: 3072})
2021-12-27 21:51:25 +08:00
rc.releaseMemory(3072)
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{})
2021-12-27 21:51:25 +08:00
2022-01-06 16:35:06 +08:00
if err := rc.reserveMemory(1024, network.ReservationPriorityLow); err != nil {
t.Fatal(err)
}
checkResources(t, &rc, network.ScopeStat{Memory: 1024})
if err := rc.reserveMemory(1024, network.ReservationPriorityLow); err == nil {
t.Fatal("expected memory check to fail")
}
checkResources(t, &rc, network.ScopeStat{Memory: 1024})
if err := rc.reserveMemory(1024, network.ReservationPriorityMedium); err != nil {
t.Fatal(err)
}
checkResources(t, &rc, network.ScopeStat{Memory: 2048})
if err := rc.reserveMemory(1024, network.ReservationPriorityMedium); err == nil {
t.Fatal("expected memory check to fail")
}
checkResources(t, &rc, network.ScopeStat{Memory: 2048})
if err := rc.reserveMemory(1024, network.ReservationPriorityHigh); err != nil {
t.Fatal(err)
}
checkResources(t, &rc, network.ScopeStat{Memory: 3072})
if err := rc.reserveMemory(512, network.ReservationPriorityHigh); err == nil {
t.Fatal("expected memory check to fail")
}
checkResources(t, &rc, network.ScopeStat{Memory: 3072})
if err := rc.reserveMemory(512, network.ReservationPriorityAlways); err != nil {
t.Fatal(err)
}
checkResources(t, &rc, network.ScopeStat{Memory: 3584})
rc.releaseMemory(3584)
checkResources(t, &rc, network.ScopeStat{})
2022-01-15 02:10:18 +08:00
// test addStream
2021-12-28 02:49:49 +08:00
if err := rc.addStream(network.DirInbound); err != nil {
2021-12-27 21:51:25 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{NumStreamsInbound: 1})
2021-12-27 21:51:25 +08:00
2021-12-28 02:49:49 +08:00
if err := rc.addStream(network.DirInbound); err == nil {
2021-12-27 21:51:25 +08:00
t.Fatal("expected addStream to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{NumStreamsInbound: 1})
2021-12-28 02:49:49 +08:00
2022-01-15 02:10:18 +08:00
if err := rc.addStream(network.DirOutbound); err == nil {
t.Fatal("expected addStream to fail")
}
checkResources(t, &rc, network.ScopeStat{NumStreamsInbound: 1})
2021-12-28 02:49:49 +08:00
rc.removeStream(network.DirInbound)
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{})
2021-12-28 02:49:49 +08:00
if err := rc.addStream(network.DirOutbound); err != nil {
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{NumStreamsOutbound: 1})
2021-12-28 02:49:49 +08:00
if err := rc.addStream(network.DirOutbound); err == nil {
2021-12-27 21:51:25 +08:00
t.Fatal("expected addStream to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{NumStreamsOutbound: 1})
2021-12-27 21:51:25 +08:00
2022-01-15 02:10:18 +08:00
if err := rc.addStream(network.DirInbound); err == nil {
t.Fatal("expected addStream to fail")
}
checkResources(t, &rc, network.ScopeStat{NumStreamsOutbound: 1})
2021-12-28 02:49:49 +08:00
rc.removeStream(network.DirOutbound)
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{})
2021-12-27 21:51:25 +08:00
2022-01-06 16:35:06 +08:00
// test addConn
2021-12-30 01:39:09 +08:00
if err := rc.addConn(network.DirInbound, false); err != nil {
2021-12-27 21:51:25 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{NumConnsInbound: 1})
2021-12-27 21:51:25 +08:00
2021-12-30 01:39:09 +08:00
if err := rc.addConn(network.DirInbound, false); err == nil {
2021-12-27 21:51:25 +08:00
t.Fatal("expected addConn to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{NumConnsInbound: 1})
2021-12-28 02:49:49 +08:00
2021-12-30 01:39:09 +08:00
rc.removeConn(network.DirInbound, false)
checkResources(t, &rc, network.ScopeStat{})
2021-12-28 02:49:49 +08:00
2021-12-30 01:39:09 +08:00
if err := rc.addConn(network.DirOutbound, false); err != nil {
2021-12-28 02:49:49 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{NumConnsOutbound: 1})
2021-12-28 02:49:49 +08:00
2021-12-30 01:39:09 +08:00
if err := rc.addConn(network.DirOutbound, false); err == nil {
2021-12-27 21:51:25 +08:00
t.Fatal("expected addConn to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{NumConnsOutbound: 1})
2021-12-27 21:51:25 +08:00
2021-12-30 01:39:09 +08:00
rc.removeConn(network.DirOutbound, false)
checkResources(t, &rc, network.ScopeStat{})
2021-12-27 21:51:25 +08:00
2021-12-30 01:39:09 +08:00
if err := rc.addConn(network.DirInbound, true); err != nil {
2021-12-27 21:51:25 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
2021-12-27 21:51:25 +08:00
2021-12-30 01:39:09 +08:00
if err := rc.addConn(network.DirOutbound, true); err == nil {
t.Fatal("expected addConn to fail")
2021-12-27 21:51:25 +08:00
}
2021-12-30 01:39:09 +08:00
checkResources(t, &rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
2021-12-27 21:51:25 +08:00
2021-12-30 01:39:09 +08:00
rc.removeConn(network.DirInbound, true)
checkResources(t, &rc, network.ScopeStat{})
2021-12-27 21:51:25 +08:00
}
2021-12-28 04:47:38 +08:00
2021-12-28 06:27:25 +08:00
func TestResourceScopeSimple(t *testing.T) {
2021-12-30 17:37:11 +08:00
s := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{
Memory: 4096,
StreamsInbound: 1,
StreamsOutbound: 1,
Streams: 1,
ConnsInbound: 1,
ConnsOutbound: 1,
Conns: 1,
FD: 1,
2021-12-28 04:47:38 +08:00
},
2022-02-12 19:59:50 +08:00
nil, "test", nil, nil,
2021-12-28 04:47:38 +08:00
)
s.IncRef()
if s.refCnt != 1 {
t.Fatal("expected refcnt of 1")
}
s.DecRef()
if s.refCnt != 0 {
t.Fatal("expected refcnt of 0")
}
2021-12-28 06:27:25 +08:00
testResourceScopeBasic(t, s)
}
2021-12-30 17:37:11 +08:00
func testResourceScopeBasic(t *testing.T, s *resourceScope) {
2022-01-06 16:27:23 +08:00
if err := s.ReserveMemory(2048, network.ReservationPriorityAlways); err != nil {
2021-12-28 04:47:38 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{Memory: 2048})
2022-01-05 16:06:11 +08:00
2022-01-06 16:27:23 +08:00
if err := s.ReserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2022-01-05 16:06:11 +08:00
t.Fatal(err)
}
checkResources(t, &s.rc, network.ScopeStat{Memory: 3072})
2022-01-06 16:27:23 +08:00
if err := s.ReserveMemory(512, network.ReservationPriorityAlways); err != nil {
2022-01-05 16:06:11 +08:00
t.Fatal(err)
}
checkResources(t, &s.rc, network.ScopeStat{Memory: 3584})
2021-12-28 04:47:38 +08:00
2022-01-06 16:27:23 +08:00
if err := s.ReserveMemory(512, network.ReservationPriorityAlways); err != nil {
2021-12-28 04:47:38 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{Memory: 4096})
2021-12-28 04:47:38 +08:00
2022-01-06 16:27:23 +08:00
if err := s.ReserveMemory(1024, network.ReservationPriorityAlways); err == nil {
2021-12-28 04:47:38 +08:00
t.Fatal("expected ReserveMemory to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{Memory: 4096})
2021-12-28 04:47:38 +08:00
s.ReleaseMemory(4096)
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{})
2021-12-28 04:47:38 +08:00
if err := s.AddStream(network.DirInbound); err != nil {
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{NumStreamsInbound: 1})
2021-12-28 04:47:38 +08:00
if err := s.AddStream(network.DirInbound); err == nil {
t.Fatal("expected AddStream to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{NumStreamsInbound: 1})
2021-12-28 04:47:38 +08:00
2022-01-15 02:10:18 +08:00
if err := s.AddStream(network.DirOutbound); err == nil {
t.Fatal("expected AddStream to fail")
}
checkResources(t, &s.rc, network.ScopeStat{NumStreamsInbound: 1})
2021-12-28 04:47:38 +08:00
s.RemoveStream(network.DirInbound)
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{})
2021-12-28 04:47:38 +08:00
if err := s.AddStream(network.DirOutbound); err != nil {
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{NumStreamsOutbound: 1})
2021-12-28 04:47:38 +08:00
if err := s.AddStream(network.DirOutbound); err == nil {
t.Fatal("expected AddStream to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{NumStreamsOutbound: 1})
2021-12-28 04:47:38 +08:00
2022-01-15 02:10:18 +08:00
if err := s.AddStream(network.DirInbound); err == nil {
t.Fatal("expected AddStream to fail")
}
checkResources(t, &s.rc, network.ScopeStat{NumStreamsOutbound: 1})
2021-12-28 04:47:38 +08:00
s.RemoveStream(network.DirOutbound)
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{})
2021-12-28 04:47:38 +08:00
2021-12-30 01:39:09 +08:00
if err := s.AddConn(network.DirInbound, false); err != nil {
2021-12-28 04:47:38 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{NumConnsInbound: 1})
2021-12-28 04:47:38 +08:00
2021-12-30 01:39:09 +08:00
if err := s.AddConn(network.DirInbound, false); err == nil {
2021-12-28 04:47:38 +08:00
t.Fatal("expected AddConn to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{NumConnsInbound: 1})
2021-12-28 04:47:38 +08:00
2021-12-30 01:39:09 +08:00
s.RemoveConn(network.DirInbound, false)
checkResources(t, &s.rc, network.ScopeStat{})
2021-12-28 04:47:38 +08:00
2021-12-30 01:39:09 +08:00
if err := s.AddConn(network.DirOutbound, false); err != nil {
2021-12-28 04:47:38 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{NumConnsOutbound: 1})
2021-12-28 04:47:38 +08:00
2021-12-30 01:39:09 +08:00
if err := s.AddConn(network.DirOutbound, false); err == nil {
2021-12-28 04:47:38 +08:00
t.Fatal("expected AddConn to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{NumConnsOutbound: 1})
2021-12-28 04:47:38 +08:00
2021-12-30 01:39:09 +08:00
s.RemoveConn(network.DirOutbound, false)
checkResources(t, &s.rc, network.ScopeStat{})
2021-12-28 04:47:38 +08:00
2021-12-30 01:39:09 +08:00
if err := s.AddConn(network.DirInbound, true); err != nil {
2021-12-28 04:47:38 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
2021-12-28 04:47:38 +08:00
2021-12-30 01:39:09 +08:00
if err := s.AddConn(network.DirOutbound, true); err == nil {
t.Fatal("expected AddConn to fail")
2021-12-28 04:47:38 +08:00
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
2021-12-28 04:47:38 +08:00
2021-12-30 01:39:09 +08:00
s.RemoveConn(network.DirInbound, true)
checkResources(t, &s.rc, network.ScopeStat{})
2021-12-28 04:47:38 +08:00
}
2021-12-28 06:27:25 +08:00
func TestResourceScopeTxnBasic(t *testing.T) {
2021-12-30 17:37:11 +08:00
s := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{
Memory: 4096,
StreamsInbound: 1,
StreamsOutbound: 1,
Streams: 1,
ConnsInbound: 1,
ConnsOutbound: 1,
Conns: 1,
FD: 1,
2021-12-28 06:27:25 +08:00
},
2022-02-12 19:59:50 +08:00
nil, "test", nil, nil,
2021-12-28 06:27:25 +08:00
)
2022-01-14 18:25:55 +08:00
txn, err := s.BeginSpan()
2021-12-28 06:27:25 +08:00
if err != nil {
t.Fatal(err)
}
2021-12-30 17:37:11 +08:00
testResourceScopeBasic(t, txn.(*resourceScope))
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{})
2021-12-28 06:27:25 +08:00
// check constraint propagation
2022-01-06 16:27:23 +08:00
if err := txn.ReserveMemory(4096, network.ReservationPriorityAlways); err != nil {
2021-12-28 06:27:25 +08:00
t.Fatal(err)
}
2021-12-30 17:37:11 +08:00
checkResources(t, &txn.(*resourceScope).rc, network.ScopeStat{Memory: 4096})
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{Memory: 4096})
2021-12-28 06:27:25 +08:00
txn.Done()
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{})
2021-12-28 06:27:25 +08:00
txn.Done() // idempotent
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{})
2021-12-28 06:27:25 +08:00
}
func TestResourceScopeTxnZombie(t *testing.T) {
2021-12-30 17:37:11 +08:00
s := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{
Memory: 4096,
StreamsInbound: 1,
StreamsOutbound: 1,
Streams: 1,
ConnsInbound: 1,
ConnsOutbound: 1,
Conns: 1,
FD: 1,
2021-12-28 06:27:25 +08:00
},
2022-02-12 19:59:50 +08:00
nil, "test", nil, nil,
2021-12-28 06:27:25 +08:00
)
2022-01-14 18:25:55 +08:00
txn1, err := s.BeginSpan()
2021-12-28 06:27:25 +08:00
if err != nil {
t.Fatal(err)
}
2022-01-14 18:25:55 +08:00
txn2, err := txn1.BeginSpan()
2021-12-28 06:27:25 +08:00
if err != nil {
t.Fatal(err)
}
2022-01-06 16:27:23 +08:00
if err := txn2.ReserveMemory(4096, network.ReservationPriorityAlways); err != nil {
2021-12-28 06:27:25 +08:00
t.Fatal(err)
}
2021-12-30 17:37:11 +08:00
checkResources(t, &txn2.(*resourceScope).rc, network.ScopeStat{Memory: 4096})
checkResources(t, &txn1.(*resourceScope).rc, network.ScopeStat{Memory: 4096})
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{Memory: 4096})
2021-12-28 06:27:25 +08:00
txn1.Done()
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{})
2022-01-06 16:27:23 +08:00
if err := txn2.ReserveMemory(4096, network.ReservationPriorityAlways); err == nil {
2021-12-28 18:20:50 +08:00
t.Fatal("expected ReserveMemory to fail")
}
2021-12-28 06:27:25 +08:00
txn2.Done()
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{})
2021-12-28 06:27:25 +08:00
}
2021-12-28 18:20:50 +08:00
func TestResourceScopeTxnTree(t *testing.T) {
2021-12-30 17:37:11 +08:00
s := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{
Memory: 4096,
StreamsInbound: 1,
StreamsOutbound: 1,
Streams: 1,
ConnsInbound: 1,
ConnsOutbound: 1,
Conns: 1,
FD: 1,
2021-12-28 18:20:50 +08:00
},
2022-02-12 19:59:50 +08:00
nil, "test", nil, nil,
2021-12-28 18:20:50 +08:00
)
2022-01-14 18:25:55 +08:00
txn1, err := s.BeginSpan()
2021-12-28 18:20:50 +08:00
if err != nil {
t.Fatal(err)
}
2022-01-14 18:25:55 +08:00
txn2, err := txn1.BeginSpan()
2021-12-28 18:20:50 +08:00
if err != nil {
t.Fatal(err)
}
2022-01-14 18:25:55 +08:00
txn3, err := txn1.BeginSpan()
2021-12-28 18:20:50 +08:00
if err != nil {
t.Fatal(err)
}
2022-01-14 18:25:55 +08:00
txn4, err := txn2.BeginSpan()
2021-12-28 18:20:50 +08:00
if err != nil {
t.Fatal(err)
}
2022-01-14 18:25:55 +08:00
txn5, err := txn2.BeginSpan()
2021-12-28 18:20:50 +08:00
if err != nil {
t.Fatal(err)
}
2022-01-06 16:27:23 +08:00
if err := txn3.ReserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2021-12-28 18:20:50 +08:00
t.Fatal(err)
}
2021-12-30 17:37:11 +08:00
checkResources(t, &txn3.(*resourceScope).rc, network.ScopeStat{Memory: 1024})
checkResources(t, &txn1.(*resourceScope).rc, network.ScopeStat{Memory: 1024})
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{Memory: 1024})
2021-12-28 18:20:50 +08:00
2022-01-06 16:27:23 +08:00
if err := txn4.ReserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2021-12-28 18:20:50 +08:00
t.Fatal(err)
}
2021-12-30 17:37:11 +08:00
checkResources(t, &txn4.(*resourceScope).rc, network.ScopeStat{Memory: 1024})
checkResources(t, &txn3.(*resourceScope).rc, network.ScopeStat{Memory: 1024})
checkResources(t, &txn2.(*resourceScope).rc, network.ScopeStat{Memory: 1024})
checkResources(t, &txn1.(*resourceScope).rc, network.ScopeStat{Memory: 2048})
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{Memory: 2048})
2021-12-28 18:20:50 +08:00
2022-01-06 16:27:23 +08:00
if err := txn5.ReserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2021-12-28 18:20:50 +08:00
t.Fatal(err)
}
2021-12-30 17:37:11 +08:00
checkResources(t, &txn5.(*resourceScope).rc, network.ScopeStat{Memory: 1024})
checkResources(t, &txn4.(*resourceScope).rc, network.ScopeStat{Memory: 1024})
checkResources(t, &txn3.(*resourceScope).rc, network.ScopeStat{Memory: 1024})
checkResources(t, &txn2.(*resourceScope).rc, network.ScopeStat{Memory: 2048})
checkResources(t, &txn1.(*resourceScope).rc, network.ScopeStat{Memory: 3072})
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{Memory: 3072})
2021-12-28 18:20:50 +08:00
2022-01-06 16:27:23 +08:00
if err := txn1.ReserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2021-12-28 18:20:50 +08:00
t.Fatal(err)
}
2021-12-30 17:37:11 +08:00
checkResources(t, &txn5.(*resourceScope).rc, network.ScopeStat{Memory: 1024})
checkResources(t, &txn4.(*resourceScope).rc, network.ScopeStat{Memory: 1024})
checkResources(t, &txn3.(*resourceScope).rc, network.ScopeStat{Memory: 1024})
checkResources(t, &txn2.(*resourceScope).rc, network.ScopeStat{Memory: 2048})
checkResources(t, &txn1.(*resourceScope).rc, network.ScopeStat{Memory: 4096})
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{Memory: 4096})
2021-12-28 18:20:50 +08:00
2022-01-06 16:27:23 +08:00
if err := txn5.ReserveMemory(1024, network.ReservationPriorityAlways); err == nil {
2021-12-28 18:20:50 +08:00
t.Fatal("expected ReserveMemory to fail")
}
2022-01-06 16:27:23 +08:00
if err := txn4.ReserveMemory(1024, network.ReservationPriorityAlways); err == nil {
2021-12-28 18:20:50 +08:00
t.Fatal("expected ReserveMemory to fail")
}
2022-01-06 16:27:23 +08:00
if err := txn3.ReserveMemory(1024, network.ReservationPriorityAlways); err == nil {
2021-12-28 18:20:50 +08:00
t.Fatal("expected ReserveMemory to fail")
}
2022-01-06 16:27:23 +08:00
if err := txn2.ReserveMemory(1024, network.ReservationPriorityAlways); err == nil {
2021-12-28 18:20:50 +08:00
t.Fatal("expected ReserveMemory to fail")
}
2021-12-30 17:37:11 +08:00
checkResources(t, &txn5.(*resourceScope).rc, network.ScopeStat{Memory: 1024})
checkResources(t, &txn4.(*resourceScope).rc, network.ScopeStat{Memory: 1024})
checkResources(t, &txn3.(*resourceScope).rc, network.ScopeStat{Memory: 1024})
checkResources(t, &txn2.(*resourceScope).rc, network.ScopeStat{Memory: 2048})
checkResources(t, &txn1.(*resourceScope).rc, network.ScopeStat{Memory: 4096})
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{Memory: 4096})
2021-12-28 18:20:50 +08:00
txn1.Done()
2021-12-30 01:39:09 +08:00
checkResources(t, &s.rc, network.ScopeStat{})
2021-12-28 18:20:50 +08:00
}
2021-12-28 18:33:34 +08:00
func TestResourceScopeDAG(t *testing.T) {
// A small DAG of scopes
// s1
// +---> s2
// +------------> s5
// +----
// +---> s3 +. \
// | \ -----+-> s4 (a diamond!)
// | ------/
// \
// ------> s6
2021-12-30 17:37:11 +08:00
s1 := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{
Memory: 4096,
StreamsInbound: 4,
StreamsOutbound: 4,
Streams: 4,
ConnsInbound: 4,
ConnsOutbound: 4,
Conns: 4,
FD: 4,
2021-12-28 18:33:34 +08:00
},
2022-02-12 19:59:50 +08:00
nil, "test", nil, nil,
2021-12-28 18:33:34 +08:00
)
2021-12-30 17:37:11 +08:00
s2 := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{
Memory: 2048,
StreamsInbound: 2,
StreamsOutbound: 2,
Streams: 2,
ConnsInbound: 2,
ConnsOutbound: 2,
Conns: 2,
FD: 2,
2021-12-28 18:33:34 +08:00
},
2022-02-12 19:59:50 +08:00
[]*resourceScope{s1}, "test", nil, nil,
2021-12-28 18:33:34 +08:00
)
2021-12-30 17:37:11 +08:00
s3 := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{
Memory: 2048,
StreamsInbound: 2,
StreamsOutbound: 2,
Streams: 2,
ConnsInbound: 2,
ConnsOutbound: 2,
Conns: 2,
FD: 2,
2021-12-28 18:33:34 +08:00
},
2022-02-12 19:59:50 +08:00
[]*resourceScope{s1}, "test", nil, nil,
2021-12-28 18:33:34 +08:00
)
2021-12-30 17:37:11 +08:00
s4 := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{
Memory: 2048,
StreamsInbound: 2,
StreamsOutbound: 2,
Streams: 2,
ConnsInbound: 2,
ConnsOutbound: 2,
Conns: 2,
FD: 2,
2021-12-28 18:33:34 +08:00
},
2022-02-12 19:59:50 +08:00
[]*resourceScope{s2, s3, s1}, "test", nil, nil,
2021-12-28 18:33:34 +08:00
)
2021-12-30 17:37:11 +08:00
s5 := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{
Memory: 2048,
StreamsInbound: 2,
StreamsOutbound: 2,
Streams: 2,
ConnsInbound: 2,
ConnsOutbound: 2,
Conns: 2,
FD: 2,
2021-12-28 18:33:34 +08:00
},
2022-02-12 19:59:50 +08:00
[]*resourceScope{s2, s1}, "test", nil, nil,
2021-12-28 18:33:34 +08:00
)
2021-12-30 17:37:11 +08:00
s6 := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{
Memory: 2048,
StreamsInbound: 2,
StreamsOutbound: 2,
Streams: 2,
ConnsInbound: 2,
ConnsOutbound: 2,
Conns: 2,
FD: 2,
2021-12-28 19:10:19 +08:00
},
2022-02-12 19:59:50 +08:00
[]*resourceScope{s3, s1}, "test", nil, nil,
2021-12-28 19:10:19 +08:00
)
2022-01-06 16:27:23 +08:00
if err := s4.ReserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2021-12-28 19:10:19 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s3.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s2.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s1.rc, network.ScopeStat{Memory: 1024})
2021-12-28 19:10:19 +08:00
2022-01-06 16:27:23 +08:00
if err := s5.ReserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2021-12-28 19:10:19 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s4.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s3.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s2.rc, network.ScopeStat{Memory: 2048})
checkResources(t, &s1.rc, network.ScopeStat{Memory: 2048})
2021-12-28 19:10:19 +08:00
2022-01-06 16:27:23 +08:00
if err := s6.ReserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2021-12-28 19:10:19 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s5.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s4.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s3.rc, network.ScopeStat{Memory: 2048})
checkResources(t, &s2.rc, network.ScopeStat{Memory: 2048})
checkResources(t, &s1.rc, network.ScopeStat{Memory: 3072})
2021-12-28 19:10:19 +08:00
2022-01-06 16:27:23 +08:00
if err := s4.ReserveMemory(1024, network.ReservationPriorityAlways); err == nil {
2021-12-28 19:10:19 +08:00
t.Fatal("expcted ReserveMemory to fail")
}
2022-01-06 16:27:23 +08:00
if err := s5.ReserveMemory(1024, network.ReservationPriorityAlways); err == nil {
2021-12-28 19:10:19 +08:00
t.Fatal("expcted ReserveMemory to fail")
}
2022-01-06 16:27:23 +08:00
if err := s6.ReserveMemory(1024, network.ReservationPriorityAlways); err == nil {
2021-12-28 19:10:19 +08:00
t.Fatal("expcted ReserveMemory to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s5.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s4.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s3.rc, network.ScopeStat{Memory: 2048})
checkResources(t, &s2.rc, network.ScopeStat{Memory: 2048})
checkResources(t, &s1.rc, network.ScopeStat{Memory: 3072})
2021-12-28 19:10:19 +08:00
s4.ReleaseMemory(1024)
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s5.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s2.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s1.rc, network.ScopeStat{Memory: 2048})
2021-12-28 19:10:19 +08:00
s5.ReleaseMemory(1024)
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s2.rc, network.ScopeStat{})
checkResources(t, &s1.rc, network.ScopeStat{Memory: 1024})
2021-12-28 19:10:19 +08:00
s6.ReleaseMemory(1024)
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{})
checkResources(t, &s2.rc, network.ScopeStat{})
checkResources(t, &s1.rc, network.ScopeStat{})
2021-12-28 19:10:19 +08:00
if err := s4.AddStream(network.DirInbound); err != nil {
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s1.rc, network.ScopeStat{NumStreamsInbound: 1})
2021-12-28 19:10:19 +08:00
if err := s5.AddStream(network.DirInbound); err != nil {
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{NumStreamsInbound: 2})
checkResources(t, &s1.rc, network.ScopeStat{NumStreamsInbound: 2})
2021-12-28 19:10:19 +08:00
if err := s6.AddStream(network.DirInbound); err != nil {
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumStreamsInbound: 2})
checkResources(t, &s2.rc, network.ScopeStat{NumStreamsInbound: 2})
checkResources(t, &s1.rc, network.ScopeStat{NumStreamsInbound: 3})
2021-12-28 19:10:19 +08:00
if err := s4.AddStream(network.DirInbound); err == nil {
t.Fatal("expected AddStream to fail")
}
if err := s5.AddStream(network.DirInbound); err == nil {
t.Fatal("expected AddStream to fail")
}
if err := s6.AddStream(network.DirInbound); err == nil {
t.Fatal("expected AddStream to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumStreamsInbound: 2})
checkResources(t, &s2.rc, network.ScopeStat{NumStreamsInbound: 2})
checkResources(t, &s1.rc, network.ScopeStat{NumStreamsInbound: 3})
2021-12-28 19:10:19 +08:00
s4.RemoveStream(network.DirInbound)
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s1.rc, network.ScopeStat{NumStreamsInbound: 2})
2021-12-28 19:10:19 +08:00
s5.RemoveStream(network.DirInbound)
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{NumStreamsInbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{})
checkResources(t, &s1.rc, network.ScopeStat{NumStreamsInbound: 1})
2021-12-28 19:10:19 +08:00
s6.RemoveStream(network.DirInbound)
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{})
checkResources(t, &s2.rc, network.ScopeStat{})
checkResources(t, &s1.rc, network.ScopeStat{})
2021-12-28 19:10:19 +08:00
if err := s4.AddStream(network.DirOutbound); err != nil {
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s1.rc, network.ScopeStat{NumStreamsOutbound: 1})
2021-12-28 19:10:19 +08:00
if err := s5.AddStream(network.DirOutbound); err != nil {
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{NumStreamsOutbound: 2})
checkResources(t, &s1.rc, network.ScopeStat{NumStreamsOutbound: 2})
2021-12-28 19:10:19 +08:00
if err := s6.AddStream(network.DirOutbound); err != nil {
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumStreamsOutbound: 2})
checkResources(t, &s2.rc, network.ScopeStat{NumStreamsOutbound: 2})
checkResources(t, &s1.rc, network.ScopeStat{NumStreamsOutbound: 3})
2021-12-28 19:10:19 +08:00
if err := s4.AddStream(network.DirOutbound); err == nil {
t.Fatal("expected AddStream to fail")
}
if err := s5.AddStream(network.DirOutbound); err == nil {
t.Fatal("expected AddStream to fail")
}
if err := s6.AddStream(network.DirOutbound); err == nil {
t.Fatal("expected AddStream to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumStreamsOutbound: 2})
checkResources(t, &s2.rc, network.ScopeStat{NumStreamsOutbound: 2})
checkResources(t, &s1.rc, network.ScopeStat{NumStreamsOutbound: 3})
2021-12-28 19:10:19 +08:00
s4.RemoveStream(network.DirOutbound)
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s1.rc, network.ScopeStat{NumStreamsOutbound: 2})
2021-12-28 19:10:19 +08:00
s5.RemoveStream(network.DirOutbound)
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{NumStreamsOutbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{})
checkResources(t, &s1.rc, network.ScopeStat{NumStreamsOutbound: 1})
2021-12-28 19:10:19 +08:00
s6.RemoveStream(network.DirOutbound)
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{})
checkResources(t, &s2.rc, network.ScopeStat{})
checkResources(t, &s1.rc, network.ScopeStat{})
if err := s4.AddConn(network.DirInbound, false); err != nil {
2021-12-28 19:10:19 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsInbound: 1})
2021-12-28 19:10:19 +08:00
2021-12-30 01:39:09 +08:00
if err := s5.AddConn(network.DirInbound, false); err != nil {
2021-12-28 19:10:19 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{NumConnsInbound: 2})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsInbound: 2})
2021-12-28 19:10:19 +08:00
2021-12-30 01:39:09 +08:00
if err := s6.AddConn(network.DirInbound, false); err != nil {
2021-12-28 19:10:19 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsInbound: 2})
checkResources(t, &s2.rc, network.ScopeStat{NumConnsInbound: 2})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsInbound: 3})
2021-12-28 19:10:19 +08:00
2021-12-30 01:39:09 +08:00
if err := s4.AddConn(network.DirInbound, false); err == nil {
2021-12-28 19:10:19 +08:00
t.Fatal("expected AddConn to fail")
}
2021-12-30 01:39:09 +08:00
if err := s5.AddConn(network.DirInbound, false); err == nil {
2021-12-28 19:10:19 +08:00
t.Fatal("expected AddConn to fail")
}
2021-12-30 01:39:09 +08:00
if err := s6.AddConn(network.DirInbound, false); err == nil {
2021-12-28 19:10:19 +08:00
t.Fatal("expected AddConn to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsInbound: 2})
checkResources(t, &s2.rc, network.ScopeStat{NumConnsInbound: 2})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsInbound: 3})
s4.RemoveConn(network.DirInbound, false)
checkResources(t, &s6.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsInbound: 2})
s5.RemoveConn(network.DirInbound, false)
checkResources(t, &s6.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsInbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsInbound: 1})
s6.RemoveConn(network.DirInbound, false)
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{})
checkResources(t, &s2.rc, network.ScopeStat{})
checkResources(t, &s1.rc, network.ScopeStat{})
if err := s4.AddConn(network.DirOutbound, false); err != nil {
2021-12-28 19:10:19 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsOutbound: 1})
2021-12-28 19:10:19 +08:00
2021-12-30 01:39:09 +08:00
if err := s5.AddConn(network.DirOutbound, false); err != nil {
2021-12-28 19:10:19 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{NumConnsOutbound: 2})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsOutbound: 2})
2021-12-28 19:10:19 +08:00
2021-12-30 01:39:09 +08:00
if err := s6.AddConn(network.DirOutbound, false); err != nil {
2021-12-28 19:10:19 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsOutbound: 2})
checkResources(t, &s2.rc, network.ScopeStat{NumConnsOutbound: 2})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsOutbound: 3})
2021-12-28 19:10:19 +08:00
2021-12-30 01:39:09 +08:00
if err := s4.AddConn(network.DirOutbound, false); err == nil {
2021-12-28 19:10:19 +08:00
t.Fatal("expected AddConn to fail")
}
2021-12-30 01:39:09 +08:00
if err := s5.AddConn(network.DirOutbound, false); err == nil {
2021-12-28 19:10:19 +08:00
t.Fatal("expected AddConn to fail")
}
2021-12-30 01:39:09 +08:00
if err := s6.AddConn(network.DirOutbound, false); err == nil {
2021-12-28 19:10:19 +08:00
t.Fatal("expected AddConn to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsOutbound: 2})
checkResources(t, &s2.rc, network.ScopeStat{NumConnsOutbound: 2})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsOutbound: 3})
s4.RemoveConn(network.DirOutbound, false)
checkResources(t, &s6.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsOutbound: 2})
s5.RemoveConn(network.DirOutbound, false)
checkResources(t, &s6.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsOutbound: 1})
checkResources(t, &s2.rc, network.ScopeStat{})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsOutbound: 1})
s6.RemoveConn(network.DirOutbound, false)
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{})
checkResources(t, &s2.rc, network.ScopeStat{})
checkResources(t, &s1.rc, network.ScopeStat{})
if err := s4.AddConn(network.DirInbound, true); err != nil {
2021-12-28 19:10:19 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s2.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
2021-12-28 19:10:19 +08:00
2021-12-30 01:39:09 +08:00
if err := s5.AddConn(network.DirInbound, true); err != nil {
2021-12-28 19:10:19 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s4.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s2.rc, network.ScopeStat{NumConnsInbound: 2, NumFD: 2})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsInbound: 2, NumFD: 2})
2021-12-28 19:10:19 +08:00
2021-12-30 01:39:09 +08:00
if err := s6.AddConn(network.DirInbound, true); err != nil {
2021-12-28 19:10:19 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s5.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s4.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsInbound: 2, NumFD: 2})
checkResources(t, &s2.rc, network.ScopeStat{NumConnsInbound: 2, NumFD: 2})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsInbound: 3, NumFD: 3})
2021-12-28 19:10:19 +08:00
2021-12-30 01:39:09 +08:00
if err := s4.AddConn(network.DirOutbound, true); err == nil {
2021-12-28 19:10:19 +08:00
t.Fatal("expected AddConn to fail")
}
2021-12-30 01:39:09 +08:00
if err := s5.AddConn(network.DirOutbound, true); err == nil {
2021-12-28 19:10:19 +08:00
t.Fatal("expected AddConn to fail")
}
2021-12-30 01:39:09 +08:00
if err := s6.AddConn(network.DirOutbound, true); err == nil {
2021-12-28 19:10:19 +08:00
t.Fatal("expected AddConn to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s5.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s4.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsInbound: 2, NumFD: 2})
checkResources(t, &s2.rc, network.ScopeStat{NumConnsInbound: 2, NumFD: 2})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsInbound: 3, NumFD: 3})
s4.RemoveConn(network.DirInbound, true)
checkResources(t, &s6.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s5.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s2.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsInbound: 2, NumFD: 2})
s5.RemoveConn(network.DirInbound, true)
checkResources(t, &s6.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
checkResources(t, &s2.rc, network.ScopeStat{})
checkResources(t, &s1.rc, network.ScopeStat{NumConnsInbound: 1, NumFD: 1})
s6.RemoveConn(network.DirInbound, true)
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{})
checkResources(t, &s2.rc, network.ScopeStat{})
checkResources(t, &s1.rc, network.ScopeStat{})
2021-12-28 19:10:19 +08:00
}
func TestResourceScopeDAGTxn(t *testing.T) {
// A small DAG of scopes
// s1
// +---> s2
// +------------> s5
// +----
// +---> s3 +. \
// | \ -----+-> s4 (a diamond!)
// | ------/
// \
// ------> s6
2021-12-30 17:37:11 +08:00
s1 := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{Memory: 8192},
2022-02-12 19:59:50 +08:00
nil, "test", nil, nil,
2021-12-28 19:10:19 +08:00
)
2021-12-30 17:37:11 +08:00
s2 := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{Memory: 4096 + 2048},
2022-02-12 19:59:50 +08:00
[]*resourceScope{s1}, "test", nil, nil,
2021-12-28 19:10:19 +08:00
)
2021-12-30 17:37:11 +08:00
s3 := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{Memory: 4096 + 2048},
2022-02-12 19:59:50 +08:00
[]*resourceScope{s1}, "test", nil, nil,
2021-12-28 19:10:19 +08:00
)
2021-12-30 17:37:11 +08:00
s4 := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{Memory: 4096 + 1024},
2022-02-12 19:59:50 +08:00
[]*resourceScope{s2, s3, s1}, "test", nil, nil,
2021-12-28 19:10:19 +08:00
)
2021-12-30 17:37:11 +08:00
s5 := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{Memory: 4096 + 1024},
2022-02-12 19:59:50 +08:00
[]*resourceScope{s2, s1}, "test", nil, nil,
2021-12-28 19:10:19 +08:00
)
2021-12-30 17:37:11 +08:00
s6 := newResourceScope(
2022-06-10 22:01:03 +08:00
&BaseLimit{Memory: 4096 + 1024},
2022-02-12 19:59:50 +08:00
[]*resourceScope{s3, s1}, "test", nil, nil,
2021-12-28 18:33:34 +08:00
)
2022-01-14 18:25:55 +08:00
txn4, err := s4.BeginSpan()
2021-12-28 18:33:34 +08:00
if err != nil {
t.Fatal(err)
}
2022-01-14 18:25:55 +08:00
txn5, err := s5.BeginSpan()
2021-12-28 18:33:34 +08:00
if err != nil {
t.Fatal(err)
}
2022-01-14 18:25:55 +08:00
txn6, err := s6.BeginSpan()
2021-12-28 18:33:34 +08:00
if err != nil {
t.Fatal(err)
}
2022-01-06 16:27:23 +08:00
if err := txn4.ReserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2021-12-28 18:33:34 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s4.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s3.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s2.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s1.rc, network.ScopeStat{Memory: 1024})
2021-12-28 18:33:34 +08:00
2022-01-06 16:27:23 +08:00
if err := txn5.ReserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2021-12-28 18:33:34 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s5.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s4.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s3.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s2.rc, network.ScopeStat{Memory: 2048})
checkResources(t, &s1.rc, network.ScopeStat{Memory: 2048})
2021-12-28 18:33:34 +08:00
2022-01-06 16:27:23 +08:00
if err := txn6.ReserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2021-12-28 18:33:34 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s5.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s4.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s3.rc, network.ScopeStat{Memory: 2048})
checkResources(t, &s2.rc, network.ScopeStat{Memory: 2048})
checkResources(t, &s1.rc, network.ScopeStat{Memory: 3072})
2021-12-28 18:33:34 +08:00
2022-01-06 16:27:23 +08:00
if err := txn4.ReserveMemory(4096, network.ReservationPriorityAlways); err != nil {
2021-12-28 18:33:34 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s5.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s4.rc, network.ScopeStat{Memory: 1024 + 4096})
checkResources(t, &s3.rc, network.ScopeStat{Memory: 2048 + 4096})
checkResources(t, &s2.rc, network.ScopeStat{Memory: 2048 + 4096})
checkResources(t, &s1.rc, network.ScopeStat{Memory: 3072 + 4096})
2021-12-28 18:33:34 +08:00
2022-01-06 16:27:23 +08:00
if err := txn4.ReserveMemory(1024, network.ReservationPriorityAlways); err == nil {
2021-12-28 18:33:34 +08:00
t.Fatal("expected ReserveMemory to fail")
}
2022-01-06 16:27:23 +08:00
if err := txn5.ReserveMemory(1024, network.ReservationPriorityAlways); err == nil {
2021-12-28 18:33:34 +08:00
t.Fatal("expected ReserveMemory to fail")
}
2022-01-06 16:27:23 +08:00
if err := txn6.ReserveMemory(1024, network.ReservationPriorityAlways); err == nil {
2021-12-28 18:33:34 +08:00
t.Fatal("expected ReserveMemory to fail")
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s5.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s4.rc, network.ScopeStat{Memory: 1024 + 4096})
checkResources(t, &s3.rc, network.ScopeStat{Memory: 2048 + 4096})
checkResources(t, &s2.rc, network.ScopeStat{Memory: 2048 + 4096})
checkResources(t, &s1.rc, network.ScopeStat{Memory: 3072 + 4096})
2021-12-28 18:33:34 +08:00
txn4.Done()
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s5.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s2.rc, network.ScopeStat{Memory: 1024})
checkResources(t, &s1.rc, network.ScopeStat{Memory: 2048})
2021-12-28 18:33:34 +08:00
2022-01-06 16:27:23 +08:00
if err := txn5.ReserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2021-12-28 18:33:34 +08:00
t.Fatal(err)
}
2022-01-06 16:27:23 +08:00
if err := txn6.ReserveMemory(1024, network.ReservationPriorityAlways); err != nil {
2021-12-28 18:33:34 +08:00
t.Fatal(err)
}
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{Memory: 2048})
checkResources(t, &s5.rc, network.ScopeStat{Memory: 2048})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{Memory: 2048})
checkResources(t, &s2.rc, network.ScopeStat{Memory: 2048})
checkResources(t, &s1.rc, network.ScopeStat{Memory: 4096})
2021-12-28 18:33:34 +08:00
txn5.Done()
txn6.Done()
2021-12-30 01:39:09 +08:00
checkResources(t, &s6.rc, network.ScopeStat{})
checkResources(t, &s5.rc, network.ScopeStat{})
checkResources(t, &s4.rc, network.ScopeStat{})
checkResources(t, &s3.rc, network.ScopeStat{})
checkResources(t, &s2.rc, network.ScopeStat{})
checkResources(t, &s1.rc, network.ScopeStat{})
2021-12-28 18:33:34 +08:00
}