Prechádzať zdrojové kódy

Add the possibility to introduce latency to the mock broker

Willem van Bergen 10 rokov pred
rodič
commit
d8f64f7bde
1 zmenil súbory, kde vykonal 10 pridanie a 0 odobranie
  1. 10 0
      mockbroker.go

+ 10 - 0
mockbroker.go

@@ -6,6 +6,7 @@ import (
 	"io"
 	"net"
 	"strconv"
+	"time"
 )
 
 // TestState is a generic interface for a test state, implemented e.g. by testing.T
@@ -33,6 +34,11 @@ type MockBroker struct {
 	expectations chan encoder
 	listener     net.Listener
 	t            TestState
+	latency      time.Duration
+}
+
+func (b *MockBroker) SetLatency(latency time.Duration) {
+	b.latency = latency
 }
 
 func (b *MockBroker) BrokerID() int32 {
@@ -80,6 +86,10 @@ func (b *MockBroker) serverLoop() (ok bool) {
 			return b.serverError(err, conn)
 		}
 
+		if b.latency > 0 {
+			time.Sleep(b.latency)
+		}
+
 		response, err := encode(expectation)
 		if err != nil {
 			return false