diff options
Diffstat (limited to 'src/hub')
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5store_test.go | 102 |
1 files changed, 49 insertions, 53 deletions
diff --git a/src/hub/src/spreadspace.org/sfive/s5store_test.go b/src/hub/src/spreadspace.org/sfive/s5store_test.go index 4c74a81..3ce13c0 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store_test.go +++ b/src/hub/src/spreadspace.org/sfive/s5store_test.go @@ -45,7 +45,17 @@ import ( ) var ( - __boltPath = "/run/s5hub_testing_db.bolt" + testBoltPath = "/run/s5hub_testing_db.bolt" + + streamIdData = StreamId{ContentId: "talkingheads", Format: "7bitascii", Quality: "high"} + sourceData = SourceId{Hostname: "streamer", Tags: []string{"tag1", "master"}, StreamId: streamIdData} + updateData = DataUpdate{Data: SourceData{ClientCount: 3, BytesReceived: 42, BytesSent: 136}, Duration: 5000} + clientsData = []ClientData{ + ClientData{"127.0.0.1", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0", 6400}, + ClientData{"10.12.0.1", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36", 6400}, + ClientData{"127.0.0.1", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0", 6400}, + ClientData{"192.168.0.1", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36", 6400}, + ClientData{"172.16.0.2", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36", 6400}} ) func TestMain(m *testing.M) { @@ -53,7 +63,7 @@ func TestMain(m *testing.M) { if err != nil { os.Exit(-1) } - __boltPath = fmt.Sprintf("/run/user/%s/s5hub_testing_db.bolt", u.Uid) + testBoltPath = fmt.Sprintf("/run/user/%s/s5hub_testing_db.bolt", u.Uid) os.Exit(m.Run()) } @@ -64,29 +74,29 @@ func TestOpen(t *testing.T) { } // store path is a directory - os.Remove(__boltPath) - if err := os.MkdirAll(__boltPath, 0700); err != nil { + os.Remove(testBoltPath) + if err := os.MkdirAll(testBoltPath, 0700); err != nil { t.Fatalf("unexpected error: %v", err) } - if _, err := NewStore(__boltPath, false); err == nil { + if _, err := NewStore(testBoltPath, false); err == nil { t.Fatalf("opening store using a directory should throw an error") } // exisitng but non-database file - os.Remove(__boltPath) - if f, err := os.Create(__boltPath); err != nil { + os.Remove(testBoltPath) + if f, err := os.Create(testBoltPath); err != nil { t.Fatalf("unexpected error: %v", err) } else { io.WriteString(f, "this is not a bolt db.") f.Close() } - if _, err := NewStore(__boltPath, false); err == nil { + if _, err := NewStore(testBoltPath, false); err == nil { t.Fatalf("opening store using a invalid database should throw an error") } // bolt db with wrong layout - os.Remove(__boltPath) - if db, err := bolt.Open(__boltPath, 0600, &bolt.Options{Timeout: 100 * time.Millisecond}); err != nil { + os.Remove(testBoltPath) + if db, err := bolt.Open(testBoltPath, 0600, &bolt.Options{Timeout: 100 * time.Millisecond}); err != nil { t.Fatalf("unexpected error: %v", err) } else { err = db.Update(func(tx *bolt.Tx) error { @@ -102,13 +112,13 @@ func TestOpen(t *testing.T) { t.Fatalf("unexpected error: %v", err) } } - if _, err := NewStore(__boltPath, false); err == nil { + if _, err := NewStore(testBoltPath, false); err == nil { t.Fatalf("opening store using a invalid database should throw an error") } // bolt db with wrong version - os.Remove(__boltPath) - if db, err := bolt.Open(__boltPath, 0600, &bolt.Options{Timeout: 100 * time.Millisecond}); err != nil { + os.Remove(testBoltPath) + if db, err := bolt.Open(testBoltPath, 0600, &bolt.Options{Timeout: 100 * time.Millisecond}); err != nil { t.Fatalf("unexpected error: %v", err) } else { err = db.Update(func(tx *bolt.Tx) error { @@ -129,13 +139,13 @@ func TestOpen(t *testing.T) { t.Fatalf("unexpected error: %v", err) } } - if _, err := NewStore(__boltPath, false); err == nil { + if _, err := NewStore(testBoltPath, false); err == nil { t.Fatalf("opening store with the wrong database version should throw an error") } // bolt db empty UUID - os.Remove(__boltPath) - if db, err := bolt.Open(__boltPath, 0600, &bolt.Options{Timeout: 100 * time.Millisecond}); err != nil { + os.Remove(testBoltPath) + if db, err := bolt.Open(testBoltPath, 0600, &bolt.Options{Timeout: 100 * time.Millisecond}); err != nil { t.Fatalf("unexpected error: %v", err) } else { err = db.Update(func(tx *bolt.Tx) error { @@ -160,20 +170,20 @@ func TestOpen(t *testing.T) { t.Fatalf("unexpected error: %v", err) } } - if _, err := NewStore(__boltPath, false); err == nil { + if _, err := NewStore(testBoltPath, false); err == nil { t.Fatalf("opening store with empty UUID should throw an error") } // create new bolt-db and reopen it - os.Remove(__boltPath) - store, err := NewStore(__boltPath, false) + os.Remove(testBoltPath) + store, err := NewStore(testBoltPath, false) if err != nil { t.Fatalf("creating new store failed: %v", err) } createdUuid := store.hubUuid store.Close() - store, err = NewStore(__boltPath, false) + store, err = NewStore(testBoltPath, false) if err != nil { t.Fatalf("re-opening existing store failed: %v", err) } @@ -181,28 +191,24 @@ func TestOpen(t *testing.T) { t.Fatalf("UUID of opened store differs from the one previously generated: '%s' != '%s'", createdUuid, store.hubUuid) } - if _, err := NewStore(__boltPath, false); err == nil { + if _, err := NewStore(testBoltPath, false); err == nil { t.Fatalf("opening already opened database should throw an error") } store.Close() } func TestAppendAndFetch(t *testing.T) { - os.Remove(__boltPath) - store, err := NewStore(__boltPath, false) + os.Remove(testBoltPath) + store, err := NewStore(testBoltPath, false) if err != nil { t.Fatalf("unexpected error: %v", err) } defer store.Close() - startTime := time.Date(2014, time.August, 24, 14, 35, 33, 847000000, time.UTC) - streamId := StreamId{ContentId: "talkingheads", Format: "7bitascii", Quality: "high"} - source := SourceId{Hostname: "streamer", Tags: []string{"tag1", "master"}, StreamId: streamId} - clients := []ClientData{ClientData{Ip: "1.2.3.4", UserAgent: "Mozilla Firefox", BytesSent: 48}, - ClientData{Ip: "5.6.7.8", UserAgent: "Google Chrome", BytesSent: 52}, - ClientData{Ip: "4.3.2.1", UserAgent: "Apple Safari", BytesSent: 36}} - update := DataUpdate{Data: SourceData{ClientCount: 3, BytesReceived: 42, BytesSent: 136, Clients: clients}, StartTime: startTime, Duration: 5000} - in := DataUpdateFull{0, "", -1, source, update} + upd := updateData + upd.StartTime = time.Date(2014, time.August, 24, 14, 35, 33, 847000000, time.UTC) + upd.Data.Clients = clientsData + in := DataUpdateFull{0, "", -1, sourceData, upd} if err = store.Append(in); err != nil { t.Fatalf("Failed to append update: %v", err) @@ -224,8 +230,7 @@ func TestAppendAndFetch(t *testing.T) { } // remote dataupdate - in = DataUpdateFull{0, "7411836d-58f7-4a36-85d6-409b4e4ca30c", 3, source, update} - + in = DataUpdateFull{0, "7411836d-58f7-4a36-85d6-409b4e4ca30c", 3, sourceData, upd} if err = store.Append(in); err != nil { t.Fatalf("Failed to append update: %v", err) } @@ -249,19 +254,19 @@ func TestAppendAndFetch(t *testing.T) { func TestReadOnly(t *testing.T) { // create read-only db from not-existing file must fail - os.Remove(__boltPath) - if _, err := NewStore(__boltPath, true); err == nil { + os.Remove(testBoltPath) + if _, err := NewStore(testBoltPath, true); err == nil { t.Fatalf("creating a read-only database should throw an error") } // open read-only db from existing file must succeed - store, err := NewStore(__boltPath, false) + store, err := NewStore(testBoltPath, false) if err != nil { t.Fatalf("unexpected error: %v", err) } store.Close() - store, err = NewStore(__boltPath, false) + store, err = NewStore(testBoltPath, false) if err != nil { t.Fatalf("opening existing store in read-only mode failed: %v", err) } @@ -289,12 +294,6 @@ func generateDataUpdateFull(n int) (data []DataUpdateFull) { numcombis := len(hostnames) * len(contents) * len(formats) * len(qualities) - clients := []ClientData{ - ClientData{"127.0.0.1", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0", 6400}, - ClientData{"10.12.0.1", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36", 6400}, - ClientData{"127.0.0.1", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0", 6400}, - ClientData{"192.168.0.1", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36", 6400}, - ClientData{"172.16.0.2", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36", 6400}} starttime := time.Now() duration := int64(15000) tags := []string{"2017", "elevate", "discourse"} @@ -315,13 +314,10 @@ func generateDataUpdateFull(n int) (data []DataUpdateFull) { d.DataUpdate.StartTime = starttime d.DataUpdate.Duration = duration - d.DataUpdate.Data.ClientCount = uint(len(clients)) - d.DataUpdate.Data.BytesSent = 6400 * uint(len(clients)) + d.DataUpdate.Data.ClientCount = uint(len(clientsData)) + d.DataUpdate.Data.BytesSent = 6400 * uint(len(clientsData)) - for _, client := range clients { - c := ClientData{client.Ip, client.UserAgent, client.BytesSent} - d.DataUpdate.Data.Clients = append(d.DataUpdate.Data.Clients, c) - } + d.DataUpdate.Data.Clients = clientsData data = append(data, d) } } @@ -333,8 +329,8 @@ func generateDataUpdateFull(n int) (data []DataUpdateFull) { } func BenchmarkAppendMany(b *testing.B) { - os.Remove(__boltPath) - store, err := NewStore(__boltPath, false) + os.Remove(testBoltPath) + store, err := NewStore(testBoltPath, false) if err != nil { b.Fatalf("Failed to initialize: %v", err) } @@ -349,8 +345,8 @@ func BenchmarkAppendMany(b *testing.B) { } func BenchmarkGetUpdatesAfter(b *testing.B) { - os.Remove(__boltPath) - store, err := NewStore(__boltPath, false) + os.Remove(testBoltPath) + store, err := NewStore(testBoltPath, false) if err != nil { b.Fatalf("Failed to initialize: %v", err) } |