Skip to content

Instantly share code, notes, and snippets.

@risentveber
Last active September 2, 2020 16:37
Show Gist options
  • Save risentveber/b3d6bcdf61d3cabbfe29637f1623a638 to your computer and use it in GitHub Desktop.
Save risentveber/b3d6bcdf61d3cabbfe29637f1623a638 to your computer and use it in GitHub Desktop.
package dynamicqueue
import (
"fmt"
"strconv"
"sync"
"testing"
"time"
)
type handler struct {
sync.Mutex
names []string
}
func (h *handler) OnItem(item interface{}) {
time.Sleep(time.Second)
h.Lock()
defer h.Unlock()
fmt.Println("process", item)
h.names = append(h.names, item.(string))
}
func TestNewDynamicQueue(t *testing.T) {
h := &handler{}
q := NewDynamicQueue(3, h)
for i := 0; i < 10; i++ {
q.Add(strconv.Itoa(i))
fmt.Println("add", i)
}
q.Stop()
if len(h.names) != 10 {
t.Errorf("invalid count %d %s", len(h.names), h.names)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment