2020-10-08 10:12:26 +00:00
|
|
|
package connection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
2021-01-14 22:33:36 +00:00
|
|
|
"time"
|
2020-10-08 10:12:26 +00:00
|
|
|
|
2021-07-28 08:27:05 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
2020-10-08 10:12:26 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-07-28 08:27:05 +00:00
|
|
|
func TestSendUrl(t *testing.T) {
|
|
|
|
observer := NewObserver(&log, &log, false)
|
|
|
|
|
|
|
|
observer.sendURL("my-url.com")
|
|
|
|
assert.Equal(t, 1.0, getCounterValue(t, observer.metrics.userHostnamesCounts, "https://my-url.com"))
|
|
|
|
|
|
|
|
observer.sendURL("https://another-long-one.com")
|
|
|
|
assert.Equal(t, 1.0, getCounterValue(t, observer.metrics.userHostnamesCounts, "https://another-long-one.com"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCounterValue(t *testing.T, metric *prometheus.CounterVec, val string) float64 {
|
|
|
|
var m = &dto.Metric{}
|
|
|
|
err := metric.WithLabelValues(val).Write(m)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
return m.Counter.GetValue()
|
|
|
|
}
|
|
|
|
|
2020-10-08 10:12:26 +00:00
|
|
|
func TestRegisterServerLocation(t *testing.T) {
|
2021-01-14 22:33:36 +00:00
|
|
|
m := newTunnelMetrics()
|
2020-10-08 10:12:26 +00:00
|
|
|
tunnels := 20
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(tunnels)
|
|
|
|
for i := 0; i < tunnels; i++ {
|
|
|
|
go func(i int) {
|
|
|
|
id := strconv.Itoa(i)
|
|
|
|
m.registerServerLocation(id, "LHR")
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
for i := 0; i < tunnels; i++ {
|
|
|
|
id := strconv.Itoa(i)
|
|
|
|
assert.Equal(t, "LHR", m.oldServerLocations[id])
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(tunnels)
|
|
|
|
for i := 0; i < tunnels; i++ {
|
|
|
|
go func(i int) {
|
|
|
|
id := strconv.Itoa(i)
|
|
|
|
m.registerServerLocation(id, "AUS")
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
for i := 0; i < tunnels; i++ {
|
|
|
|
id := strconv.Itoa(i)
|
|
|
|
assert.Equal(t, "AUS", m.oldServerLocations[id])
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-01-14 22:33:36 +00:00
|
|
|
|
|
|
|
func TestObserverEventsDontBlock(t *testing.T) {
|
2021-02-03 18:32:54 +00:00
|
|
|
observer := NewObserver(&log, &log, false)
|
2021-01-14 22:33:36 +00:00
|
|
|
var mu sync.Mutex
|
|
|
|
observer.RegisterSink(EventSinkFunc(func(_ Event) {
|
|
|
|
// callback will block if lock is already held
|
|
|
|
mu.Lock()
|
|
|
|
mu.Unlock()
|
|
|
|
}))
|
|
|
|
|
|
|
|
timeout := time.AfterFunc(5*time.Second, func() {
|
2021-01-19 12:20:11 +00:00
|
|
|
mu.Unlock() // release the callback on timer expiration
|
2021-01-14 22:33:36 +00:00
|
|
|
t.Fatal("observer is blocked")
|
|
|
|
})
|
|
|
|
|
2021-01-19 12:20:11 +00:00
|
|
|
mu.Lock() // block the callback
|
|
|
|
for i := 0; i < 2*observerChannelBufferSize; i++ {
|
|
|
|
observer.sendRegisteringEvent(0)
|
2021-01-14 22:33:36 +00:00
|
|
|
}
|
|
|
|
if pending := timeout.Stop(); pending {
|
|
|
|
// release the callback if timer hasn't expired yet
|
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
2021-02-04 21:09:17 +00:00
|
|
|
|
|
|
|
type eventCollectorSink struct {
|
|
|
|
observedEvents []Event
|
2021-03-23 14:30:43 +00:00
|
|
|
mu sync.Mutex
|
2021-02-04 21:09:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *eventCollectorSink) OnTunnelEvent(event Event) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.observedEvents = append(s.observedEvents, event)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *eventCollectorSink) assertSawEvent(t *testing.T, event Event) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
assert.Contains(t, s.observedEvents, event)
|
2021-03-23 14:30:43 +00:00
|
|
|
}
|