github.com/blend/go-sdk@v1.20220411.3/web/timeout_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package web
     9  
    10  import (
    11  	"net/http"
    12  	"sync/atomic"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/blend/go-sdk/assert"
    17  )
    18  
    19  func TestTimeout(t *testing.T) {
    20  	t.Skip() // flaky
    21  	assert := assert.New(t)
    22  
    23  	app := MustNew(
    24  		OptBindAddr(DefaultMockBindAddr),
    25  		OptUse(WithTimeout(1*time.Millisecond)),
    26  	)
    27  
    28  	var didShortFinish, didLongFinish int32
    29  	app.GET("/panic", func(_ *Ctx) Result {
    30  		panic("test")
    31  	})
    32  	app.GET("/long", func(_ *Ctx) Result {
    33  		time.Sleep(5 * time.Millisecond)
    34  		atomic.StoreInt32(&didLongFinish, 1)
    35  		return NoContent
    36  	})
    37  	app.GET("/short", func(_ *Ctx) Result {
    38  		atomic.StoreInt32(&didShortFinish, 1)
    39  		return NoContent
    40  	})
    41  
    42  	go func() { _ = app.Start() }()
    43  	defer func() { _ = app.Stop() }()
    44  	<-app.NotifyStarted()
    45  
    46  	res, err := http.Get("http://" + app.Listener.Addr().String() + "/panic")
    47  	assert.Nil(err)
    48  	assert.Nil(res.Body.Close())
    49  
    50  	res, err = http.Get("http://" + app.Listener.Addr().String() + "/long")
    51  	assert.Nil(err)
    52  	assert.Nil(res.Body.Close())
    53  	assert.Zero(atomic.LoadInt32(&didLongFinish))
    54  
    55  	res, err = http.Get("http://" + app.Listener.Addr().String() + "/short")
    56  	assert.Nil(err)
    57  	assert.Nil(res.Body.Close())
    58  	assert.Equal(1, atomic.LoadInt32(&didShortFinish))
    59  }