-
Notifications
You must be signed in to change notification settings - Fork 678
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
Merged
Changes from 6 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,151 @@ | ||
package syslog | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
"text/template" | ||
"time" | ||
|
||
docker "github.com/fsouza/go-dockerclient" | ||
"github.com/gliderlabs/logspout/router" | ||
"github.com/gliderlabs/logspout/testutil" | ||
|
||
_ "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}}" | ||
) | ||
|
||
var ( | ||
container = &docker.Container{ | ||
ID: "8dfafdbc3a40", | ||
Name: "\x00michaelshobbs", | ||
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) { | ||
os.Setenv("RETRY_COUNT", strconv.Itoa(int(1))) | ||
setRetryCount() | ||
defer func() { | ||
os.Unsetenv("RETRY_COUNT") | ||
setRetryCount() | ||
}() | ||
tmpl, err := template.New("syslog").Parse(testTmplStr) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
ls, err := testutil.NewLocalTCPServer() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer ls.Teardown() | ||
|
||
route := &router.Route{ | ||
ID: "0", | ||
Adapter: "syslog", | ||
Address: ls.Listener.Addr().String(), | ||
} | ||
|
||
datac := make(chan []byte, testutil.MaxMsgCount) | ||
errc := make(chan error, 1) | ||
go testutil.AcceptAndCloseConn(ls, datac, errc) | ||
|
||
adapter := &Adapter{ | ||
route: route, | ||
conn: testutil.Dial(ls), | ||
tmpl: tmpl, | ||
transport: testutil.MockTransport{Listener: ls}, | ||
} | ||
|
||
logstream := make(chan *router.Message) | ||
done := make(chan bool) | ||
sentMsgs := [][]byte{} | ||
// Send msgs to logstream | ||
go sendLogstream(logstream, done, &sentMsgs, tmpl) | ||
|
||
// Stream logstream to conn | ||
go adapter.Stream(logstream) | ||
|
||
// Check for errs from goroutines | ||
for err := range errc { | ||
t.Errorf("%v", err) | ||
} | ||
|
||
readMsgs := [][]byte{} | ||
for { | ||
select { | ||
case <-done: | ||
if testutil.MaxMsgCount-1 != len(datac) { | ||
t.Errorf("expected %v got %v", testutil.MaxMsgCount-1, len(datac)) | ||
} | ||
for msg := range datac { | ||
readMsgs = append(readMsgs, msg) | ||
} | ||
sentMsgs = append(sentMsgs[:testutil.CloseOnMsgIdx], sentMsgs[testutil.CloseOnMsgIdx+1:]...) | ||
for i, v := range sentMsgs { | ||
sent := strings.Trim(fmt.Sprintf("%s", v), "\n") | ||
read := strings.Trim(fmt.Sprintf("%s", readMsgs[i]), "\x00\n") | ||
if sent != read { | ||
t.Errorf("expected %+q got %+q", sent, read) | ||
} | ||
} | ||
} | ||
return | ||
} | ||
} | ||
|
||
func sendLogstream(logstream chan *router.Message, done chan bool, msgs *[][]byte, tmpl *template.Template) { | ||
defer func() { | ||
close(logstream) | ||
close(done) | ||
}() | ||
var count int | ||
for { | ||
if count == testutil.MaxMsgCount { | ||
done <- true | ||
return | ||
} | ||
msg := &router.Message{ | ||
Container: container, | ||
Data: "hellooo", | ||
Time: time.Now(), | ||
} | ||
m := &Message{msg} | ||
buf, _ := m.Render(tmpl) | ||
*msgs = append(*msgs, buf) | ||
logstream <- msg | ||
count++ | ||
time.Sleep(1000 * time.Millisecond) | ||
} | ||
} |
Oops, something went wrong.
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