-
Notifications
You must be signed in to change notification settings - Fork 681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
preserve buffer on reconnect() AND reconnect on connection reset by peer #291
Merged
+191
−22
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bfb5a2a
make test target more flexible
michaelshobbs 68e3094
attempt to preserve buffer on reconnect()
michaelshobbs d12e81e
race detector for alpine is broken. disable it for now
michaelshobbs 43c0db0
add test for reconnect()
michaelshobbs 00668f8
use mock conn for syslog tests
michaelshobbs ea5f535
make vet more reliable
michaelshobbs 2ad7bbf
don't use a mock conn yet. thanks @mattaitchison
michaelshobbs 78c6d3d
Don't retry sending on ECONNRESET
luketurner bb848d5
add some reasonable delay to log stream
michaelshobbs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,22 +1,175 @@ | ||
package syslog | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net" | ||
"os" | ||
"strconv" | ||
"sync" | ||
"testing" | ||
"text/template" | ||
"time" | ||
|
||
docker "github.com/fsouza/go-dockerclient" | ||
"github.com/gliderlabs/logspout/router" | ||
|
||
_ "github.com/gliderlabs/logspout/transports/tcp" | ||
_ "github.com/gliderlabs/logspout/transports/tls" | ||
_ "github.com/gliderlabs/logspout/transports/udp" | ||
) | ||
|
||
func TestSyslogRetryCount(t *testing.T) { | ||
setRetryCount() | ||
if retryCount != defaultRetryCount { | ||
t.Errorf("expected %v got %v", defaultRetryCount, retryCount) | ||
const ( | ||
testPriority = "{{.Priority}}" | ||
testTimestamp = "{{.Timestamp}}" | ||
testHostname = "{{.Container.Config.Hostname}}" | ||
testTag = "{{.ContainerName}}" | ||
testPid = "{{.Container.State.Pid}}" | ||
testData = "{{.Data}}" | ||
connCloseIdx = 5 | ||
) | ||
|
||
var ( | ||
container = &docker.Container{ | ||
ID: "8dfafdbc3a40", | ||
Name: "\x00container", | ||
Config: &docker.Config{ | ||
Hostname: "8dfafdbc3a40", | ||
}, | ||
} | ||
testTmplStr = fmt.Sprintf("<%s>%s %s %s[%s]: %s\n", | ||
testPriority, testTimestamp, testHostname, testTag, testPid, testData) | ||
) | ||
|
||
func TestSyslogRetryCount(t *testing.T) { | ||
newRetryCount := uint(20) | ||
os.Setenv("RETRY_COUNT", strconv.Itoa(int(newRetryCount))) | ||
defer os.Unsetenv("RETRY_COUNT") | ||
setRetryCount() | ||
if retryCount != newRetryCount { | ||
t.Errorf("expected %v got %v", newRetryCount, retryCount) | ||
} | ||
|
||
os.Unsetenv("RETRY_COUNT") | ||
setRetryCount() | ||
if retryCount != defaultRetryCount { | ||
t.Errorf("expected %v got %v", defaultRetryCount, retryCount) | ||
} | ||
} | ||
|
||
func TestSyslogReconnectOnClose(t *testing.T) { | ||
done := make(chan string) | ||
addr, sock, srvWG := startServer("tcp", "", done) | ||
defer srvWG.Wait() | ||
defer os.Remove(addr) | ||
defer sock.Close() | ||
route := &router.Route{Adapter: "syslog+tcp", Address: addr} | ||
adapter, err := NewSyslogAdapter(route) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
stream := make(chan *router.Message) | ||
go adapter.Stream(stream) | ||
|
||
count := 100 | ||
messages := make(chan string, count) | ||
go sendLogstream(stream, messages, adapter, count) | ||
|
||
timeout := time.After(6 * time.Second) | ||
msgnum := 1 | ||
for { | ||
select { | ||
case msg := <-done: | ||
// Don't check a message that we know was dropped | ||
if msgnum%connCloseIdx == 0 { | ||
_ = <-messages | ||
msgnum++ | ||
} | ||
check(t, adapter.(*Adapter).tmpl, <-messages, msg) | ||
msgnum++ | ||
case <-timeout: | ||
adapter.(*Adapter).conn.Close() | ||
t.Fatal("timeout after", msgnum, "messages") | ||
return | ||
default: | ||
if msgnum == count { | ||
adapter.(*Adapter).conn.Close() | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
func startServer(n, la string, done chan<- string) (addr string, sock io.Closer, wg *sync.WaitGroup) { | ||
if n == "udp" || n == "tcp" { | ||
la = "127.0.0.1:0" | ||
} | ||
wg = new(sync.WaitGroup) | ||
|
||
l, err := net.Listen(n, la) | ||
if err != nil { | ||
log.Fatalf("startServer failed: %v", err) | ||
} | ||
addr = l.Addr().String() | ||
sock = l | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
runStreamSyslog(l, done, wg) | ||
}() | ||
|
||
return | ||
} | ||
|
||
func runStreamSyslog(l net.Listener, done chan<- string, wg *sync.WaitGroup) { | ||
for { | ||
c, err := l.Accept() | ||
if err != nil { | ||
return | ||
} | ||
wg.Add(1) | ||
go func(c net.Conn) { | ||
defer wg.Done() | ||
c.SetReadDeadline(time.Now().Add(5 * time.Second)) | ||
b := bufio.NewReader(c) | ||
var i = 1 | ||
for { | ||
i++ | ||
s, err := b.ReadString('\n') | ||
if err != nil { | ||
break | ||
} | ||
done <- s | ||
if i%connCloseIdx == 0 { | ||
break | ||
} | ||
} | ||
c.Close() | ||
}(c) | ||
} | ||
} | ||
|
||
func sendLogstream(stream chan *router.Message, messages chan string, adapter router.LogAdapter, count int) { | ||
for i := 1; i <= count; i++ { | ||
msg := &Message{ | ||
Message: &router.Message{ | ||
Container: container, | ||
Data: "test " + strconv.Itoa(i), | ||
Time: time.Now(), | ||
Source: "stdout", | ||
}, | ||
} | ||
stream <- msg.Message | ||
b, _ := msg.Render(adapter.(*Adapter).tmpl) | ||
messages <- string(b) | ||
time.Sleep(10 * time.Millisecond) | ||
} | ||
} | ||
|
||
func check(t *testing.T, tmpl *template.Template, in string, out string) { | ||
if in != out { | ||
t.Errorf("expected: %s\ngot: %s\n", in, out) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is pretty intense.
I'd recommend breaking it down a little. Maybe move the
go func()
to named functions so they're justgo someReallyClearFuncName()
Would make things a little more readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done