gitlab.com/picnic-app/backend/role-api@v0.0.0-20230614140944-06a76ff3696d/internal/util/background/context_test.go (about)

     1  package background_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"gitlab.com/picnic-app/backend/role-api/internal/util/background"
    10  )
    11  
    12  func TestBackgroundContextFrom(t *testing.T) {
    13  	var key struct{}
    14  	const value = 1
    15  	withValue := context.WithValue(context.Background(), key, value)
    16  
    17  	canceled, cancel := context.WithCancel(withValue)
    18  	cancel()
    19  
    20  	copied := background.Context(canceled)
    21  	require.NoError(t, copied.Err())
    22  
    23  	deadline, ok := copied.Deadline()
    24  	require.False(t, ok)
    25  	require.Zero(t, deadline)
    26  
    27  	select {
    28  	case <-copied.Done():
    29  		t.Fatal("should not be done")
    30  	default:
    31  	}
    32  
    33  	require.Equal(t, withValue.Value(key), copied.Value(key))
    34  }