feat(go): add 100k ingest hardening baseline
This commit is contained in:
73
go/vehicle-gateway/internal/loadsim/runner_test.go
Normal file
73
go/vehicle-gateway/internal/loadsim/runner_test.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package loadsim
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRunnerDialsTargetConnectionsAndWritesFrames(t *testing.T) {
|
||||
var writes atomic.Int64
|
||||
var closed atomic.Int64
|
||||
runner := Runner{
|
||||
Dial: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
return &recordingConn{
|
||||
write: func(p []byte) (int, error) {
|
||||
writes.Add(1)
|
||||
return len(p), nil
|
||||
},
|
||||
close: func() error {
|
||||
closed.Add(1)
|
||||
return nil
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
|
||||
stats, err := runner.Run(context.Background(), Config{
|
||||
Protocol: ProtocolJT808,
|
||||
Addr: "127.0.0.1:808",
|
||||
Connections: 3,
|
||||
ConnectRatePerSecond: 1000,
|
||||
SendInterval: time.Millisecond,
|
||||
Duration: 5 * time.Millisecond,
|
||||
Template: "0200",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Run() error = %v", err)
|
||||
}
|
||||
|
||||
if stats.ConnectionsOpened != 3 {
|
||||
t.Fatalf("ConnectionsOpened = %d, want 3", stats.ConnectionsOpened)
|
||||
}
|
||||
if stats.FramesWritten == 0 {
|
||||
t.Fatalf("FramesWritten = 0, want at least one")
|
||||
}
|
||||
if writes.Load() != int64(stats.FramesWritten) {
|
||||
t.Fatalf("recorded writes = %d, stats = %d", writes.Load(), stats.FramesWritten)
|
||||
}
|
||||
if closed.Load() != 3 {
|
||||
t.Fatalf("closed = %d, want 3", closed.Load())
|
||||
}
|
||||
}
|
||||
|
||||
type recordingConn struct {
|
||||
write func([]byte) (int, error)
|
||||
close func() error
|
||||
}
|
||||
|
||||
func (c *recordingConn) Read(p []byte) (int, error) { return 0, context.Canceled }
|
||||
func (c *recordingConn) Write(p []byte) (int, error) { return c.write(p) }
|
||||
func (c *recordingConn) Close() error { return c.close() }
|
||||
func (c *recordingConn) LocalAddr() net.Addr { return testAddr("local") }
|
||||
func (c *recordingConn) RemoteAddr() net.Addr { return testAddr("remote") }
|
||||
func (c *recordingConn) SetDeadline(t time.Time) error { return nil }
|
||||
func (c *recordingConn) SetReadDeadline(t time.Time) error { return nil }
|
||||
func (c *recordingConn) SetWriteDeadline(t time.Time) error { return nil }
|
||||
|
||||
type testAddr string
|
||||
|
||||
func (a testAddr) Network() string { return "tcp" }
|
||||
func (a testAddr) String() string { return string(a) }
|
||||
Reference in New Issue
Block a user