harness-drone/livelog/livelog_test.go
2019-02-27 23:07:13 -08:00

121 lines
2.3 KiB
Go

// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
// +build !oss
package livelog
import (
"context"
"sync"
"testing"
"github.com/drone/drone/core"
"github.com/google/go-cmp/cmp"
)
func TestStreamer(t *testing.T) {
s := New().(*streamer)
err := s.Create(context.Background(), 1)
if err != nil {
t.Error(err)
}
if len(s.streams) == 0 {
t.Errorf("Want stream registered")
}
w := sync.WaitGroup{}
w.Add(1)
go func() {
w.Add(3)
s.Write(context.Background(), 1, &core.Line{})
s.Write(context.Background(), 1, &core.Line{})
s.Write(context.Background(), 1, &core.Line{})
w.Done()
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tail, errc := s.Tail(ctx, 1)
go func() {
for {
select {
case <-errc:
return
case <-ctx.Done():
return
case <-tail:
w.Done()
}
}
}()
w.Wait()
}
func TestStreamerDelete(t *testing.T) {
s := New().(*streamer)
err := s.Create(context.Background(), 1)
if err != nil {
t.Error(err)
}
if len(s.streams) == 0 {
t.Errorf("Want stream registered")
}
err = s.Delete(context.Background(), 1)
if err != nil {
t.Error(err)
}
if len(s.streams) != 0 {
t.Errorf("Want stream unregistered")
}
}
func TestStreamerDeleteErr(t *testing.T) {
s := New()
err := s.Delete(context.Background(), 1)
if err != errStreamNotFound {
t.Errorf("Want errStreamNotFound")
}
}
func TestStreamerWriteErr(t *testing.T) {
s := New()
err := s.Write(context.Background(), 1, &core.Line{})
if err != errStreamNotFound {
t.Errorf("Want errStreamNotFound")
}
}
func TestStreamTailNotFound(t *testing.T) {
s := New()
outc, errc := s.Tail(context.Background(), 0)
if outc != nil && errc != nil {
t.Errorf("Expect nil channel when stream not found")
}
}
func TestStreamerInfo(t *testing.T) {
s := New().(*streamer)
s.streams[1] = &stream{list: map[*subscriber]struct{}{{}: struct{}{}, {}: struct{}{}}}
s.streams[2] = &stream{list: map[*subscriber]struct{}{{}: struct{}{}}}
s.streams[3] = &stream{list: map[*subscriber]struct{}{}}
got := s.Info(context.Background())
want := &core.LogStreamInfo{
Streams: map[int64]int{
1: 2,
2: 1,
3: 0,
},
}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf(diff)
}
}