Is there a common pattern for writing tests for workers? I have code that 
is something like this:

type struct Dispatcher{
work chan interface{}
moreWork chan interface{}
// some other state info stored here
}

func (d *Dispatcher) Run {
for {
select {
case w := <-work:
// Code block 1
//some code
//goes here
case m := <-moreWork:
// Code block 2
//other code
//goes here
// potentially more cases               
}
} 
}

What's the best way to go about testing it? Integration tests that just 
send data through the channels and wait for the right behaviour feel 
clumsy. I can test the code blocks in isolation if I pull them out:

func (d *Dispatcher) codeBlockOne(w interface{}) {
// Code block 1
//some code
//goes here
}

func (d *Dispatcher) codeBlockTwo(m interface{}) {
// Code block 2
//other code
//goes here
}


func (d *Dispatcher) Run {
for {
select {
case w := <-work:
d.codeBlockOne(w)
case m := <-moreWork:
d.codeBlockTwo(w)
}
} 
}

But that has the side effect that it potentially exposes the code blocks to 
other code in the package (I don't want to accidentally call them without 
the channel). Is there a cleaner way?

I'm also not sure how I test that the Run() function is dispatching to the 
right code blocks. I considered injecting an object into Run that has the 
codeBlockOne and codeBlockTwo methods on it, but now I'm doing a lot of 
refactoring away from what feels like the normal way to write this code.

What's the common way that this is tested?

Tim

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to