{agent,client: add NoopHandler

This commit is contained in:
Aleksandr Razumov
2018-08-11 20:22:21 +03:00
parent 4b75dfd8f1
commit 29b07afea1
4 changed files with 14 additions and 8 deletions

View File

@@ -6,8 +6,15 @@ import (
"time" "time"
) )
// NoopHandler just discards any event.
var NoopHandler Handler = func(e Event) {}
// NewAgent initializes and returns new Agent with provided handler. // NewAgent initializes and returns new Agent with provided handler.
// If h is nil, the NoopHandler will be used.
func NewAgent(h Handler) *Agent { func NewAgent(h Handler) *Agent {
if h == nil {
h = NoopHandler
}
a := &Agent{ a := &Agent{
transactions: make(map[transactionID]agentTransaction), transactions: make(map[transactionID]agentTransaction),
handler: h, handler: h,

View File

@@ -56,7 +56,7 @@ func TestAgent_Process(t *testing.T) {
} }
func TestAgent_Start(t *testing.T) { func TestAgent_Start(t *testing.T) {
a := NewAgent(noopHandler) a := NewAgent(nil)
id := NewTransactionID() id := NewTransactionID()
deadline := time.Now().AddDate(0, 0, 1) deadline := time.Now().AddDate(0, 0, 1)
if err := a.Start(id, deadline); err != nil { if err := a.Start(id, deadline); err != nil {
@@ -120,10 +120,8 @@ func TestAgent_Stop(t *testing.T) {
} }
} }
var noopHandler = func(e Event) {}
func TestAgent_GC(t *testing.T) { func TestAgent_GC(t *testing.T) {
a := NewAgent(noopHandler) a := NewAgent(nil)
shouldTimeOutID := make(map[transactionID]bool) shouldTimeOutID := make(map[transactionID]bool)
deadline := time.Date(2027, time.November, 21, deadline := time.Date(2027, time.November, 21,
23, 0, 0, 0, 23, 0, 0, 0,
@@ -170,7 +168,7 @@ func TestAgent_GC(t *testing.T) {
} }
func BenchmarkAgent_GC(b *testing.B) { func BenchmarkAgent_GC(b *testing.B) {
a := NewAgent(noopHandler) a := NewAgent(nil)
deadline := time.Now().AddDate(0, 0, 1) deadline := time.Now().AddDate(0, 0, 1)
for i := 0; i < agentCollectCap; i++ { for i := 0; i < agentCollectCap; i++ {
if err := a.Start(NewTransactionID(), deadline); err != nil { if err := a.Start(NewTransactionID(), deadline); err != nil {
@@ -192,7 +190,7 @@ func BenchmarkAgent_GC(b *testing.B) {
} }
func BenchmarkAgent_Process(b *testing.B) { func BenchmarkAgent_Process(b *testing.B) {
a := NewAgent(noopHandler) a := NewAgent(nil)
deadline := time.Now().AddDate(0, 0, 1) deadline := time.Now().AddDate(0, 0, 1)
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
if err := a.Start(NewTransactionID(), deadline); err != nil { if err := a.Start(NewTransactionID(), deadline); err != nil {

View File

@@ -18,3 +18,4 @@ pkg github.com/gortc/stun, type ClientOptions struct, Clock Clock
pkg github.com/gortc/stun, type ClientOptions struct, Collector Collector pkg github.com/gortc/stun, type ClientOptions struct, Collector Collector
pkg github.com/gortc/stun, type ClientOptions struct, RTO time.Duration pkg github.com/gortc/stun, type ClientOptions struct, RTO time.Duration
pkg github.com/gortc/stun, func NewAgent(Handler) *Agent pkg github.com/gortc/stun, func NewAgent(Handler) *Agent
pkg github.com/gortc/stun, var NoopHandler Handler

View File

@@ -380,7 +380,7 @@ func TestClientConnErr(t *testing.T) {
if err := c.Do(m, nil); err == nil { if err := c.Do(m, nil); err == nil {
t.Error("error expected") t.Error("error expected")
} }
if err := c.Do(m, noopHandler); err == nil { if err := c.Do(m, NoopHandler); err == nil {
t.Error("error expected") t.Error("error expected")
} }
} }
@@ -406,7 +406,7 @@ func TestClientConnErrStopErr(t *testing.T) {
} }
}() }()
m := MustBuild(TransactionID) m := MustBuild(TransactionID)
if err := c.Do(m, noopHandler); err == nil { if err := c.Do(m, NoopHandler); err == nil {
t.Error("error expected") t.Error("error expected")
} }
} }