Skip to content
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

fix(example): use context #19

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion _examples/notify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ func main() {
c.String(http.StatusOK, "Welcome Gin Server")
})

if err := router.RunWithContext(ctx); err != nil && err != context.Canceled {
go func() {
if err := router.RunWithContext(context.Background()); err != nil && err != context.Canceled {
panic(err)
}
}()

<-ctx.Done()

if err := router.Shutdown(context.Background()); err != nil {
panic(err)
}
}
82 changes: 82 additions & 0 deletions graceful_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import (
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"runtime"
"sync"
"syscall"
"testing"
"time"

Expand Down Expand Up @@ -54,6 +57,85 @@ func TestCycle(t *testing.T) {
}
}

func TestSimpleSignal(t *testing.T) {
router, err := Default()
assert.NoError(t, err)
assert.NotNil(t, router)
defer router.Close()

router.GET("/example", func(c *gin.Context) {
time.Sleep(20 * time.Second)
c.String(http.StatusOK, "it worked")
})

start := time.Now()

ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT)
defer cancel()

var wg sync.WaitGroup

wg.Add(1)
go func() {
defer wg.Done()
assert.NoError(t, router.RunWithContext(context.Background()))
}()

wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(5 * time.Second)
assert.NoError(t, syscall.Kill(syscall.Getpid(), syscall.SIGINT))
}()

wg.Add(1)
go func() {
defer wg.Done()
testRequest(t, "http://localhost:8080/example")
}()

<-ctx.Done()

assert.NoError(t, router.Shutdown(context.Background()))
assert.GreaterOrEqual(t, time.Since(start).Seconds(), 20.0)

wg.Wait()
}

func TestSimpleSleep(t *testing.T) {
router, err := Default()
assert.NoError(t, err)
assert.NotNil(t, router)
defer router.Close()

router.GET("/example", func(c *gin.Context) {
time.Sleep(20 * time.Second)
c.String(http.StatusOK, "it worked")
})

start := time.Now()
var wg sync.WaitGroup

wg.Add(1)
go func() {
defer wg.Done()
assert.NoError(t, router.RunWithContext(context.Background()))
}()

wg.Add(1)
go func() {
defer wg.Done()
testRequest(t, "http://localhost:8080/example")
}()

time.Sleep(5 * time.Second)

assert.NoError(t, router.Shutdown(context.Background()))
assert.GreaterOrEqual(t, time.Since(start).Seconds(), 20.0)

wg.Wait()
}

func TestSimpleCycle(t *testing.T) {
router, err := Default()
assert.NoError(t, err)
Expand Down
Loading