-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
192 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package collectors | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
"time" | ||
|
||
"github.com/dmachard/go-dnscollector/dnsutils" | ||
"github.com/dmachard/go-dnscollector/loggers" | ||
"github.com/dmachard/go-dnscollector/pkgconfig" | ||
"github.com/dmachard/go-dnscollector/processors" | ||
"github.com/dmachard/go-logger" | ||
) | ||
|
||
func Test_DnsMessage_BufferLoggerIsFull(t *testing.T) { | ||
// redirect stdout output to bytes buffer | ||
logsChan := make(chan logger.LogEntry, 10) | ||
lg := logger.New(true) | ||
lg.SetOutputChannel((logsChan)) | ||
|
||
// init the collector and run-it | ||
config := pkgconfig.GetFakeConfig() | ||
c := NewDNSMessage(nil, config, lg, "test") | ||
|
||
// init next logger with a buffer of one element | ||
nxt := loggers.NewFakeLoggerWithBufferSize(1) | ||
c.AddDefaultRoute(nxt) | ||
|
||
// run collector | ||
go c.Run() | ||
|
||
// add a shot of dnsmessages to collector | ||
dmIn := dnsutils.GetFakeDNSMessage() | ||
for i := 0; i < 512; i++ { | ||
c.GetInputChannel() <- dmIn | ||
} | ||
|
||
// waiting monitor to run in consumer | ||
time.Sleep(12 * time.Second) | ||
|
||
for entry := range logsChan { | ||
fmt.Println(entry) | ||
pattern := regexp.MustCompile(processors.ExpectedBufferMsg511) | ||
if pattern.MatchString(entry.Message) { | ||
break | ||
} | ||
} | ||
|
||
// read dnsmessage from next logger | ||
dmOut := <-nxt.GetInputChannel() | ||
if dmOut.DNS.Qname != "dns.collector" { | ||
t.Errorf("invalid qname in dns message: %s", dmOut.DNS.Qname) | ||
} | ||
|
||
// send second shot of packets to consumer | ||
for i := 0; i < 1024; i++ { | ||
c.GetInputChannel() <- dmIn | ||
} | ||
|
||
// waiting monitor to run in consumer | ||
time.Sleep(12 * time.Second) | ||
|
||
for entry := range logsChan { | ||
fmt.Println(entry) | ||
pattern := regexp.MustCompile(processors.ExpectedBufferMsg1023) | ||
if pattern.MatchString(entry.Message) { | ||
break | ||
} | ||
} | ||
// read dnsmessage from next logger | ||
dm2 := <-nxt.GetInputChannel() | ||
if dm2.DNS.Qname != "dns.collector" { | ||
t.Errorf("invalid qname in dns message: %s", dm2.DNS.Qname) | ||
} | ||
|
||
// stop all | ||
c.Stop() | ||
nxt.Stop() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters