perkeep/pkg/blobserver/blobhub_test.go

134 lines
3.9 KiB
Go
Raw Permalink Normal View History

2011-02-26 20:22:26 +00:00
/*
Copyright 2011 The Perkeep Authors
2011-02-26 20:22:26 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package blobserver
import (
"testing"
"time"
"perkeep.org/pkg/blob"
2011-02-26 20:22:26 +00:00
)
func TestHubRegistration(t *testing.T) {
hub := &memHub{}
ch := make(chan blob.Ref)
ch2 := make(chan blob.Ref)
b1 := blob.MustParse("sha1-0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
b2 := blob.MustParse("sha1-62cdb7020ff920e5aa642c3d4066950dd1f01f4d")
2011-02-26 20:22:26 +00:00
if hub.listeners != nil {
t.Fatalf("hub.listeners is not nil before RegisterListener")
}
2011-02-26 20:22:26 +00:00
hub.RegisterListener(ch)
if want := 1; len(hub.listeners) != want {
t.Fatalf("len(hub.listeners) after RegisterListener: got: %d, expected: %d", len(hub.listeners), want)
}
2011-02-26 20:22:26 +00:00
hub.RegisterListener(ch2)
if want := 2; len(hub.listeners) != want {
t.Fatalf("len(hub.listeners) after ch2 RegisterListener: got: %d, expected: %d", len(hub.listeners), want)
}
2011-02-26 20:22:26 +00:00
hub.UnregisterListener(ch)
if want := 1; len(hub.listeners) != want {
t.Fatalf("len(hub.listeners) after UnregisterListener: got: %d, expected: %d", len(hub.listeners), want)
}
2011-02-26 20:22:26 +00:00
hub.UnregisterListener(ch2)
if want := 0; len(hub.listeners) != want {
t.Fatalf("len(hub.listeners) after UnregisterListener: got: %d, expected: %d", len(hub.listeners), want)
}
2011-02-26 20:22:26 +00:00
if hub.blobListeners != nil {
t.Fatalf("hub.blobListeners is not nil before RegisterBlobListener")
}
2011-02-26 20:22:26 +00:00
hub.RegisterBlobListener(b1, ch)
if hub.blobListeners == nil {
t.Fatal("hub.blobListeners is nil after RegisterBlobListener")
}
if hub.blobListeners[b1] == nil {
t.Fatal("b1 in hub.blobListeners map is nil")
}
if want := 1; len(hub.blobListeners[b1]) != want {
t.Fatalf("hub.blobListeners[b1] size: got: %d, expected: %d", len(hub.blobListeners[b1]), want)
}
if want := 1; len(hub.blobListeners) != want {
t.Fatalf("hub.blobListeners size: got: %d, expected: %d", len(hub.listeners), want)
}
2011-02-26 20:22:26 +00:00
hub.RegisterBlobListener(b2, ch)
if want := 1; len(hub.blobListeners[b2]) != want {
t.Fatalf("hub.blobListeners[b2] size: got: %d, expected: %d", len(hub.blobListeners[b2]), want)
}
if want := 2; len(hub.blobListeners) != want {
t.Fatalf("hub.blobListeners size: got: %d, expected: %d", len(hub.listeners), want)
}
2011-02-26 20:22:26 +00:00
hub.UnregisterBlobListener(b2, ch)
if hub.blobListeners[b2] != nil {
t.Fatalf("b2 in hub.blobListeners map is not nil")
}
if want := 1; len(hub.blobListeners) != want {
t.Fatalf("hub.blobListeners size: got: %d, expected: %d", len(hub.blobListeners), want)
}
2011-02-26 20:22:26 +00:00
hub.UnregisterBlobListener(b1, ch)
if hub.blobListeners[b1] != nil {
t.Fatalf("b1 in hub.blobListeners map is not nil")
}
if want := 0; len(hub.blobListeners) != want {
t.Fatalf("hub.blobListeners size: got: %d, expected: %d", len(hub.blobListeners), want)
}
2011-02-26 20:22:26 +00:00
}
func TestHubFiring(t *testing.T) {
hub := &memHub{}
ch := make(chan blob.Ref)
bch := make(chan blob.Ref)
blob1 := blob.MustParse("sha1-0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
blobsame := blob.MustParse("sha1-0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
2011-02-26 20:22:26 +00:00
hub.NotifyBlobReceived(blob.SizedRef{Ref: blob1, Size: 123}) // no-op
2011-02-26 20:22:26 +00:00
hub.RegisterListener(ch)
hub.RegisterBlobListener(blob1, bch)
2011-02-26 20:22:26 +00:00
hub.NotifyBlobReceived(blob.SizedRef{Ref: blobsame, Size: 456})
2011-02-26 20:22:26 +00:00
tmr1 := time.NewTimer(1 * time.Second)
2011-02-26 20:22:26 +00:00
select {
case <-tmr1.C:
t.Fatal("timer expired on receiving from ch")
case got := <-ch:
if got != blob1 {
2011-02-26 20:22:26 +00:00
t.Fatalf("got wrong blob")
}
}
select {
case <-tmr1.C:
t.Fatal("timer expired on receiving from bch")
case got := <-bch:
if got != blob1 {
2011-02-26 20:22:26 +00:00
t.Fatalf("got wrong blob")
}
}
tmr1.Stop()
}