coredns/plugin/dnstap/dnstapio/io_test.go
Miek Gieben a136b7128b
plugin/dnstap: remove custom encoder (#4242)
* plugin/dnstap: remove encoder*.go

Those files reimplemented parts of the dnstap spec, we can just use the
dnstap functions for that. This leaves all the queuing that is enabled
and drops messages if the dnstap reader can't keep up. In the new code
flush() would never return an error (at least I couldn't make it do so),
so the reconnect functionally is moved to kick off when we get write
errors.

Some smaller cosmetic changes as well, `d.socket` is now `proto`, which
makes the dial() function smaller.

Total testing time is now <1s (which was the impetus to look into this
plugin *again*).

See #4238
The buffered channel needs to be sized correctly, as we may need to do
some queing if the dnstap reader can't keep up.

Signed-off-by: Miek Gieben <miek@miek.nl>

* add missing file

Signed-off-by: Miek Gieben <miek@miek.nl>

* update doc on queing

Signed-off-by: Miek Gieben <miek@miek.nl>
2020-11-03 06:31:34 -08:00

157 lines
2.7 KiB
Go

package dnstapio
import (
"net"
"sync"
"testing"
"time"
"github.com/coredns/coredns/plugin/pkg/reuseport"
tap "github.com/dnstap/golang-dnstap"
fs "github.com/farsightsec/golang-framestream"
)
var (
msgType = tap.Dnstap_MESSAGE
msg = tap.Dnstap{Type: &msgType}
)
func accept(t *testing.T, l net.Listener, count int) {
server, err := l.Accept()
if err != nil {
t.Fatalf("Server accepted: %s", err)
}
dec, err := fs.NewDecoder(server, &fs.DecoderOptions{
ContentType: []byte("protobuf:dnstap.Dnstap"),
Bidirectional: true,
})
if err != nil {
t.Fatalf("Server decoder: %s", err)
}
for i := 0; i < count; i++ {
if _, err := dec.Decode(); err != nil {
t.Errorf("Server decode: %s", err)
}
}
if err := server.Close(); err != nil {
t.Error(err)
}
}
func TestTransport(t *testing.T) {
transport := [2][2]string{
{"tcp", ":0"},
{"unix", "dnstap.sock"},
}
for _, param := range transport {
// Start TCP listener
l, err := reuseport.Listen(param[0], param[1])
if err != nil {
t.Fatalf("Cannot start listener: %s", err)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
accept(t, l, 1)
wg.Done()
}()
dio := New(param[0], l.Addr().String())
dio.tcpTimeout = 10 * time.Millisecond
dio.flushTimeout = 30 * time.Millisecond
dio.Connect()
dio.Dnstap(msg)
wg.Wait()
l.Close()
dio.Close()
}
}
func TestRace(t *testing.T) {
count := 10
l, err := reuseport.Listen("tcp", ":0")
if err != nil {
t.Fatalf("Cannot start listener: %s", err)
}
defer l.Close()
var wg sync.WaitGroup
wg.Add(1)
go func() {
accept(t, l, count)
wg.Done()
}()
dio := New("tcp", l.Addr().String())
dio.tcpTimeout = 10 * time.Millisecond
dio.flushTimeout = 30 * time.Millisecond
dio.Connect()
defer dio.Close()
wg.Add(count)
for i := 0; i < count; i++ {
go func() {
msg := tap.Dnstap_MESSAGE
dio.Dnstap(tap.Dnstap{Type: &msg})
wg.Done()
}()
}
wg.Wait()
}
func TestReconnect(t *testing.T) {
count := 5
l, err := reuseport.Listen("tcp", ":0")
if err != nil {
t.Fatalf("Cannot start listener: %s", err)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
accept(t, l, 1)
wg.Done()
}()
addr := l.Addr().String()
dio := New("tcp", addr)
dio.tcpTimeout = 10 * time.Millisecond
dio.flushTimeout = 30 * time.Millisecond
dio.Connect()
defer dio.Close()
dio.Dnstap(msg)
wg.Wait()
// Close listener
l.Close()
// And start TCP listener again on the same port
l, err = reuseport.Listen("tcp", addr)
if err != nil {
t.Fatalf("Cannot start listener: %s", err)
}
defer l.Close()
wg.Add(1)
go func() {
accept(t, l, 1)
wg.Done()
}()
for i := 0; i < count; i++ {
time.Sleep(100 * time.Millisecond)
dio.Dnstap(msg)
}
wg.Wait()
}