Skip to content

Commit

Permalink
chore: small changes to TrafficLogger function names & update all moc…
Browse files Browse the repository at this point in the history
…ks to mockery v2.43.0
  • Loading branch information
tobyxdd committed May 11, 2024
1 parent 6a34a9e commit 9d4b3e6
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 66 deletions.
2 changes: 1 addition & 1 deletion app/internal/proxymux/internal/mocks/mock_Conn.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/internal/proxymux/internal/mocks/mock_Listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion core/client/mock_udpIO.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 42 additions & 42 deletions core/internal/integration_tests/mocks/mock_TrafficLogger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions core/internal/integration_tests/trafficlogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestClientServerTrafficLoggerTCP(t *testing.T) {
go s.Serve()

// Create client
trafficLogger.EXPECT().LogOnlineState("nobody", true).Return().Once()
c, _, err := client.NewClient(&client.Config{
ServerAddr: udpAddr,
TLSConfig: client.TLSConfig{InsecureSkipVerify: true},
Expand Down Expand Up @@ -66,7 +67,7 @@ func TestClientServerTrafficLoggerTCP(t *testing.T) {
assert.NoError(t, err)

// Client reads from server
trafficLogger.EXPECT().Log("nobody", uint64(0), uint64(11)).Return(true).Once()
trafficLogger.EXPECT().LogTraffic("nobody", uint64(0), uint64(11)).Return(true).Once()
sobConnCh <- []byte("knock knock")
buf := make([]byte, 100)
n, err := conn.Read(buf)
Expand All @@ -75,15 +76,16 @@ func TestClientServerTrafficLoggerTCP(t *testing.T) {
assert.Equal(t, "knock knock", string(buf[:n]))

// Client writes to server
trafficLogger.EXPECT().Log("nobody", uint64(12), uint64(0)).Return(true).Once()
trafficLogger.EXPECT().LogTraffic("nobody", uint64(12), uint64(0)).Return(true).Once()
sobConn.EXPECT().Write([]byte("who is there")).Return(12, nil).Once()
n, err = conn.Write([]byte("who is there"))
assert.NoError(t, err)
assert.Equal(t, 12, n)
time.Sleep(1 * time.Second) // Need some time for the server to receive the data

// Client reads from server again but blocked
trafficLogger.EXPECT().Log("nobody", uint64(0), uint64(4)).Return(false).Once()
trafficLogger.EXPECT().LogTraffic("nobody", uint64(0), uint64(4)).Return(false).Once()
trafficLogger.EXPECT().LogOnlineState("nobody", false).Return().Once()
sobConnCh <- []byte("nope")
n, err = conn.Read(buf)
assert.Zero(t, n)
Expand Down Expand Up @@ -116,6 +118,7 @@ func TestClientServerTrafficLoggerUDP(t *testing.T) {
go s.Serve()

// Create client
trafficLogger.EXPECT().LogOnlineState("nobody", true).Return().Once()
c, _, err := client.NewClient(&client.Config{
ServerAddr: udpAddr,
TLSConfig: client.TLSConfig{InsecureSkipVerify: true},
Expand Down Expand Up @@ -146,22 +149,23 @@ func TestClientServerTrafficLoggerUDP(t *testing.T) {
assert.NoError(t, err)

// Client writes to server
trafficLogger.EXPECT().Log("nobody", uint64(9), uint64(0)).Return(true).Once()
trafficLogger.EXPECT().LogTraffic("nobody", uint64(9), uint64(0)).Return(true).Once()
sobConn.EXPECT().WriteTo([]byte("small sad"), addr).Return(9, nil).Once()
err = conn.Send([]byte("small sad"), addr)
assert.NoError(t, err)
time.Sleep(1 * time.Second) // Need some time for the server to receive the data

// Client reads from server
trafficLogger.EXPECT().Log("nobody", uint64(0), uint64(7)).Return(true).Once()
trafficLogger.EXPECT().LogTraffic("nobody", uint64(0), uint64(7)).Return(true).Once()
sobConnCh <- []byte("big mad")
bs, rAddr, err := conn.Receive()
assert.NoError(t, err)
assert.Equal(t, rAddr, addr)
assert.Equal(t, "big mad", string(bs))

// Client reads from server again but blocked
trafficLogger.EXPECT().Log("nobody", uint64(0), uint64(4)).Return(false).Once()
trafficLogger.EXPECT().LogTraffic("nobody", uint64(0), uint64(4)).Return(false).Once()
trafficLogger.EXPECT().LogOnlineState("nobody", false).Return().Once()
sobConnCh <- []byte("nope")
bs, rAddr, err = conn.Receive()
assert.Equal(t, err, io.EOF)
Expand Down
4 changes: 2 additions & 2 deletions core/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,6 @@ type EventLogger interface {
// bandwidth limits or post-connection authentication, for example.
// The implementation of this interface must be thread-safe.
type TrafficLogger interface {
Log(id string, tx, rx uint64) (ok bool)
LogOnlineStateChanged(id string, online bool)
LogTraffic(id string, tx, rx uint64) (ok bool)
LogOnlineState(id string, online bool)
}
4 changes: 2 additions & 2 deletions core/server/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func copyTwoWayWithLogger(id string, serverRw, remoteRw io.ReadWriter, l Traffic
errChan := make(chan error, 2)
go func() {
errChan <- copyBufferLog(serverRw, remoteRw, func(n uint64) bool {
return l.Log(id, 0, n)
return l.LogTraffic(id, 0, n)
})
}()
go func() {
errChan <- copyBufferLog(remoteRw, serverRw, func(n uint64) bool {
return l.Log(id, n, 0)
return l.LogTraffic(id, n, 0)
})
}()
// Block until one of the two goroutines returns
Expand Down
14 changes: 13 additions & 1 deletion core/server/mock_UDPConn.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/server/mock_udpEventLogger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion core/server/mock_udpIO.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions core/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (s *serverImpl) handleClient(conn quic.Connection) {
// If the client is authenticated, we need to log the disconnect event
if handler.authenticated {
if tl := s.config.TrafficLogger; tl != nil {
tl.LogOnlineStateChanged(handler.authID, false)
tl.LogOnlineState(handler.authID, false)
}
if el := s.config.EventLogger; el != nil {
el.Disconnect(conn.RemoteAddr(), handler.authID, err)
Expand Down Expand Up @@ -159,7 +159,7 @@ func (h *h3sHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(protocol.StatusAuthOK)
// Call event logger
if tl := h.config.TrafficLogger; tl != nil {
tl.LogOnlineStateChanged(id, true)
tl.LogOnlineState(id, true)
}
if el := h.config.EventLogger; el != nil {
el.Connect(h.conn.RemoteAddr(), id, actualTx)
Expand Down Expand Up @@ -276,7 +276,7 @@ func (io *udpIOImpl) ReceiveMessage() (*protocol.UDPMessage, error) {
continue
}
if io.TrafficLogger != nil {
ok := io.TrafficLogger.Log(io.AuthID, uint64(len(udpMsg.Data)), 0)
ok := io.TrafficLogger.LogTraffic(io.AuthID, uint64(len(udpMsg.Data)), 0)
if !ok {
// TrafficLogger requested to disconnect the client
_ = io.Conn.CloseWithError(closeErrCodeTrafficLimitReached, "")
Expand All @@ -289,7 +289,7 @@ func (io *udpIOImpl) ReceiveMessage() (*protocol.UDPMessage, error) {

func (io *udpIOImpl) SendMessage(buf []byte, msg *protocol.UDPMessage) error {
if io.TrafficLogger != nil {
ok := io.TrafficLogger.Log(io.AuthID, 0, uint64(len(msg.Data)))
ok := io.TrafficLogger.LogTraffic(io.AuthID, 0, uint64(len(msg.Data)))
if !ok {
// TrafficLogger requested to disconnect the client
_ = io.Conn.CloseWithError(closeErrCodeTrafficLimitReached, "")
Expand Down
Loading

0 comments on commit 9d4b3e6

Please sign in to comment.