This file is indexed.

/usr/share/gocode/src/github.com/hashicorp/serf/serf/coalesce_test.go is in golang-github-hashicorp-serf-dev 0.7.0~ds1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package serf

import (
	"fmt"
	"reflect"
	"testing"
	"time"
)

// Mock EventCounter type
const EventCounter EventType = 9000

type counterEvent struct {
	delta int
}

func (c counterEvent) EventType() EventType {
	return EventCounter
}
func (c counterEvent) String() string {
	return fmt.Sprintf("CounterEvent %d", c.delta)
}

// Mock coalescer
type mockCoalesce struct {
	value int
}

func (c *mockCoalesce) Handle(e Event) bool {
	return e.EventType() == EventCounter
}

func (c *mockCoalesce) Coalesce(e Event) {
	c.value += e.(counterEvent).delta
}

func (c *mockCoalesce) Flush(outChan chan<- Event) {
	outChan <- counterEvent{c.value}
	c.value = 0
}

func testCoalescer(cPeriod, qPeriod time.Duration) (chan<- Event, <-chan Event, chan<- struct{}) {
	in := make(chan Event, 64)
	out := make(chan Event)
	shutdown := make(chan struct{})
	c := &mockCoalesce{}
	go coalesceLoop(in, out, shutdown, cPeriod, qPeriod, c)
	return in, out, shutdown
}

func TestCoalescer_basic(t *testing.T) {
	in, out, shutdown := testCoalescer(5*time.Millisecond, time.Second)
	defer close(shutdown)

	send := []Event{
		counterEvent{1},
		counterEvent{39},
		counterEvent{2},
	}
	for _, e := range send {
		in <- e
	}

	select {
	case e := <-out:
		if e.EventType() != EventCounter {
			t.Fatalf("expected counter, got: %d", e.EventType())
		}

		if e.(counterEvent).delta != 42 {
			t.Fatalf("bad: %#v", e)
		}

	case <-time.After(50 * time.Millisecond):
		t.Fatalf("timeout")
	}
}

func TestCoalescer_quiescent(t *testing.T) {
	// This tests the quiescence by creating a long coalescence period
	// with a short quiescent period and waiting only a multiple of the
	// quiescent period for results.
	in, out, shutdown := testCoalescer(10*time.Second, 10*time.Millisecond)
	defer close(shutdown)

	send := []Event{
		counterEvent{1},
		counterEvent{39},
		counterEvent{2},
	}
	for _, e := range send {
		in <- e
	}

	select {
	case e := <-out:
		if e.EventType() != EventCounter {
			t.Fatalf("expected counter, got: %d", e.EventType())
		}

		if e.(counterEvent).delta != 42 {
			t.Fatalf("bad: %#v", e)
		}
	case <-time.After(50 * time.Millisecond):
		t.Fatalf("timeout")
	}
}

func TestCoalescer_passThrough(t *testing.T) {
	in, out, shutdown := testCoalescer(time.Second, time.Second)
	defer close(shutdown)

	send := []Event{
		UserEvent{
			Name:    "test",
			Payload: []byte("foo"),
		},
	}

	for _, e := range send {
		in <- e
	}

	select {
	case e := <-out:
		if e.EventType() != EventUser {
			t.Fatalf("expected user event, got: %d", e.EventType())
		}

		if e.(UserEvent).Name != "test" {
			t.Fatalf("name should be test. %v", e)
		}

		if !reflect.DeepEqual([]byte("foo"), e.(UserEvent).Payload) {
			t.Fatalf("bad: %#v", e.(UserEvent).Payload)
		}
	case <-time.After(50 * time.Millisecond):
		t.Fatalf("timeout")
	}
}