github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/experimental/sock/sock_test.go (about)

     1  package sock_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/wasilibs/wazerox/experimental/sock"
     8  	internalsock "github.com/wasilibs/wazerox/internal/sock"
     9  	"github.com/wasilibs/wazerox/internal/testing/require"
    10  )
    11  
    12  // testCtx is an arbitrary, non-default context. Non-nil also prevents linter errors.
    13  var testCtx = context.WithValue(context.Background(), struct{}{}, "arbitrary")
    14  
    15  func TestWithSockConfig(t *testing.T) {
    16  	tests := []struct {
    17  		name     string
    18  		sockCfg  sock.Config
    19  		expected bool
    20  	}{
    21  		{
    22  			name:     "returns input when sockCfg nil",
    23  			expected: false,
    24  		},
    25  		{
    26  			name:     "returns input when sockCfg empty",
    27  			sockCfg:  sock.NewConfig(),
    28  			expected: false,
    29  		},
    30  		{
    31  			name:     "decorates with sockCfg",
    32  			sockCfg:  sock.NewConfig().WithTCPListener("", 0),
    33  			expected: true,
    34  		},
    35  	}
    36  
    37  	for _, tt := range tests {
    38  		tc := tt
    39  		t.Run(tc.name, func(t *testing.T) {
    40  			if decorated := sock.WithConfig(testCtx, tc.sockCfg); tc.expected {
    41  				require.NotNil(t, decorated.Value(internalsock.ConfigKey{}))
    42  			} else {
    43  				require.Same(t, testCtx, decorated)
    44  			}
    45  		})
    46  	}
    47  }