Parcourir la source

Merge pull request #295 from Shopify/mock_broker_latency

Add the possibility to introduce latency to the mock broker
Willem van Bergen il y a 10 ans
Parent
commit
18198dce59
1 fichiers modifiés avec 10 ajouts et 0 suppressions
  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