From 4b4695769b47134cd99e59c68cd793baaf728696 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Sat, 10 Oct 2015 13:24:16 -0700 Subject: [PATCH] made copying router, than copies messages (and contexts) it sees to memory, and then re-routes it to the sub-router it was given. --- copying_router.go | 51 +++++++++++++++ copying_router_test.go | 141 +++++++++++++++++++++++++++++++++++++++++ flog.go | 7 -- print.go | 6 -- 4 files changed, 192 insertions(+), 13 deletions(-) create mode 100644 copying_router.go create mode 100644 copying_router_test.go diff --git a/copying_router.go b/copying_router.go new file mode 100644 index 0000000..b6eeba9 --- /dev/null +++ b/copying_router.go @@ -0,0 +1,51 @@ +package flog + + +// NewCopyingRouter returns an initialized CopyingRouter. +func NewCopyingRouter(subrouter Router) *CopyingRouter { + copies := make([]struct{Message string ; Context map[string]interface{}}, 0, 8) + + router := CopyingRouter { + subrouter:subrouter, + copies:copies, + } + + return &router +} + + +// CopyingRouter is a Router that copies a message (and its context) to memory, and then +// re-routes a message (and its context) to a sub-router. +// +// This router is NOT designed to be used for an indefinite number of routings, and instead +// should only be used for a limited amount of routings, as it stores all the copies in +// memory. +type CopyingRouter struct { + subrouter Router + copies []struct{Message string ; Context map[string]interface{}} +} + + +func (router *CopyingRouter) Route(message string, context map[string]interface{}) error { + copy := struct{Message string ; Context map[string]interface{}}{ + Message: message, + Context: context, + } + router.copies = append(router.copies, copy) + + return router.subrouter.Route(message, context) +} + + +func (router *CopyingRouter) Copies() <-chan struct{Message string ; Context map[string]interface{}} { + ch := make(chan struct{Message string ; Context map[string]interface{}}) + + go func() { + for _, copy := range router.copies { + ch <- copy + } + close(ch) + }() + + return ch +} diff --git a/copying_router_test.go b/copying_router_test.go new file mode 100644 index 0000000..2c9092e --- /dev/null +++ b/copying_router_test.go @@ -0,0 +1,141 @@ +package flog + + +import ( + "testing" + + "fmt" + "math/rand" + "time" +) + + +func TestNewCopyingRouter(t *testing.T) { + + randomness := rand.New(rand.NewSource( time.Now().UTC().UnixNano() )) + + + router := NewCopyingRouter(NewDiscardRouter()) + if nil == router { + t.Errorf("After trying to create a copying router, expected it to be not nil, but was: %v", router) + } + + + lenInitialCopies := 0 + for range router.Copies() { + lenInitialCopies++ + } + if expected, actual := 0, lenInitialCopies; expected != actual { + t.Errorf("After creating a copying router, expected copies to be length %d, but was actually %d,", expected, actual) + } + + + message := fmt.Sprint("%x", randomness.Int63n(9999999999)) + + context := make(map[string]interface{}) + limit := randomness.Int63n(30) + for i:=int64(0); i