-
Notifications
You must be signed in to change notification settings - Fork 29
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
grpc graceful shutdown #64
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4363d12
graceful
ccb3041
mv yamlproto
3f67684
remove int test paths
22b7899
add utility
a20a92f
add timeout
fc65aec
lint
e4fa237
int test
e77005b
rename test
8f95e10
info logs
71c583d
revert mode placemeent
230bd6e
revert mode placemeent
d488e3d
revert mode placemeent
a521809
logger.Sync
cfa8013
fix lint
a59ea8e
cancel inside shuutdown
215b584
test blocking shutdown
0271674
lint
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// +build integration | ||
|
||
package integration | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"path" | ||
"syscall" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
binaryName = "xds-relay" | ||
) | ||
|
||
func TestServerShutdown(t *testing.T) { | ||
dir, err := os.Getwd() | ||
assert.Nil(t, err) | ||
cmd := exec.Command(path.Join(dir, "bin", binaryName), | ||
"-c", "./integration/testdata/bootstrap_configuration_complete_tech_spec.yaml", | ||
"-a", "./integration/testdata/keyer_configuration_complete_tech_spec.yaml", | ||
"-l", "debug", | ||
"-m", "serve") | ||
err = cmd.Start() | ||
assert.Nil(t, err) | ||
<-time.After(5 * time.Second) | ||
assert.Equal(t, -1, cmd.ProcessState.ExitCode()) | ||
e := cmd.Process.Signal(syscall.SIGINT) | ||
assert.Nil(t, e) | ||
err = cmd.Wait() | ||
assert.Nil(t, e) | ||
} |
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,69 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"syscall" | ||
"testing" | ||
"time" | ||
|
||
"github.com/envoyproxy/xds-relay/internal/pkg/log" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestShutdown(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
blockedCh := make(chan bool, 1) | ||
l := &logger{} | ||
registerShutdownHandler(ctx, cancel, func() { | ||
blockedCh <- true | ||
}, l, time.Second*5) | ||
_ = syscall.Kill(syscall.Getpid(), syscall.SIGINT) | ||
<-blockedCh | ||
} | ||
|
||
func TestShutdownTimeout(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
l := &logger{blockedCh: make(chan bool, 1)} | ||
registerShutdownHandler(ctx, cancel, func() { | ||
<-time.After(time.Minute) | ||
}, l, time.Millisecond) | ||
_ = syscall.Kill(syscall.Getpid(), syscall.SIGINT) | ||
<-l.blockedCh | ||
assert.Equal(t, "shutdown error: context deadline exceeded", l.lastErr) | ||
} | ||
|
||
type logger struct { | ||
blockedCh chan bool | ||
lastErr string | ||
} | ||
|
||
func (l *logger) Named(name string) log.Logger { | ||
return l | ||
} | ||
|
||
func (l *logger) With(args ...interface{}) log.Logger { | ||
return l | ||
} | ||
|
||
func (l *logger) Sync() error { return nil } | ||
|
||
func (l *logger) Debug(ctx context.Context, args ...interface{}) { | ||
} | ||
|
||
func (l *logger) Info(ctx context.Context, args ...interface{}) { | ||
} | ||
|
||
func (l *logger) Warn(ctx context.Context, args ...interface{}) { | ||
} | ||
|
||
func (l *logger) Error(ctx context.Context, args ...interface{}) { | ||
l.lastErr = fmt.Sprint(args...) | ||
l.blockedCh <- true | ||
} | ||
|
||
func (l *logger) Fatal(ctx context.Context, args ...interface{}) { | ||
} | ||
|
||
func (l *logger) Panic(ctx context.Context, args ...interface{}) { | ||
} |
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,24 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
// DoWithTimeout runs f and returns its error. | ||
// If the timeout elapses first, returns a ctx timeout error instead. | ||
func DoWithTimeout(ctx context.Context, f func() error, t time.Duration) error { | ||
timeoutCtx, cancel := context.WithTimeout(ctx, t) | ||
defer cancel() | ||
errChan := make(chan error, 1) | ||
go func() { | ||
errChan <- f() | ||
close(errChan) | ||
}() | ||
select { | ||
case <-timeoutCtx.Done(): | ||
return timeoutCtx.Err() | ||
case err := <-errChan: | ||
return err | ||
} | ||
} |
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,65 @@ | ||
package util_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/envoyproxy/xds-relay/internal/pkg/util" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestExecuteWithinTimeout(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
methodExecuted := false | ||
err := util.DoWithTimeout(ctx, func() error { | ||
methodExecuted = true | ||
return nil | ||
}, time.Second) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, methodExecuted, true) | ||
} | ||
|
||
func TestExecuteWithinTimeoutReturnsError(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
returnErr := fmt.Errorf("error") | ||
err := util.DoWithTimeout(ctx, func() error { | ||
return returnErr | ||
}, time.Second) | ||
|
||
assert.NotNil(t, err) | ||
assert.Equal(t, err, returnErr) | ||
} | ||
|
||
func TestExecuteWithinExceedsTimeout(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
err := util.DoWithTimeout(ctx, func() error { | ||
<-time.After(time.Millisecond) | ||
return nil | ||
}, time.Nanosecond) | ||
|
||
assert.NotNil(t, err) | ||
assert.Equal(t, err.Error(), "context deadline exceeded") | ||
} | ||
|
||
func TestExecuteCancel(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
methodCalled := make(chan bool, 1) | ||
go func() { | ||
<-methodCalled | ||
cancel() | ||
}() | ||
err := util.DoWithTimeout(ctx, func() error { | ||
methodCalled <- true | ||
<-time.After(time.Minute) | ||
return nil | ||
}, time.Minute) | ||
|
||
assert.NotNil(t, err) | ||
assert.Equal(t, err.Error(), "context canceled") | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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.
Is this guaranteed to be here in CI jobs? Should there be an environment variable but default to this if it is not set?
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.
yeah..the CI jobs are set up to correctly set the folder https://github.com/envoyproxy/xds-relay/blob/master/.github/workflows/integration-tests.yml#L40
They run locally too without any changes.