forked from envoyproxy/xds-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
grpc graceful shutdown (envoyproxy#64)
Signed-off-by: Jyoti Mahapatra <jmahapatra@lyft.com>
- Loading branch information
1 parent
24d87a6
commit aefa63b
Showing
33 changed files
with
238 additions
and
37 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,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