...

Source file src/go.uber.org/atomic/nocmp_test.go

Documentation: go.uber.org/atomic

     1  // Copyright (c) 2020 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package atomic
    22  
    23  import (
    24  	"bytes"
    25  	"os"
    26  	"os/exec"
    27  	"path/filepath"
    28  	"reflect"
    29  	"testing"
    30  
    31  	"github.com/stretchr/testify/assert"
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  func TestNocmpComparability(t *testing.T) {
    36  	tests := []struct {
    37  		desc       string
    38  		give       interface{}
    39  		comparable bool
    40  	}{
    41  		{
    42  			desc: "nocmp struct",
    43  			give: nocmp{},
    44  		},
    45  		{
    46  			desc: "struct with nocmp embedded",
    47  			give: struct{ nocmp }{},
    48  		},
    49  		{
    50  			desc:       "pointer to struct with nocmp embedded",
    51  			give:       &struct{ nocmp }{},
    52  			comparable: true,
    53  		},
    54  
    55  		// All exported types must be uncomparable.
    56  		{desc: "Bool", give: Bool{}},
    57  		{desc: "Duration", give: Duration{}},
    58  		{desc: "Error", give: Error{}},
    59  		{desc: "Float64", give: Float64{}},
    60  		{desc: "Int32", give: Int32{}},
    61  		{desc: "Int64", give: Int64{}},
    62  		{desc: "String", give: String{}},
    63  		{desc: "Uint32", give: Uint32{}},
    64  		{desc: "Uint64", give: Uint64{}},
    65  		{desc: "Value", give: Value{}},
    66  	}
    67  
    68  	for _, tt := range tests {
    69  		t.Run(tt.desc, func(t *testing.T) {
    70  			typ := reflect.TypeOf(tt.give)
    71  			assert.Equalf(t, tt.comparable, typ.Comparable(),
    72  				"type %v comparablity mismatch", typ)
    73  		})
    74  	}
    75  }
    76  
    77  // nocmp must not add to the size of a struct in-memory.
    78  func TestNocmpSize(t *testing.T) {
    79  	type x struct{ _ int }
    80  
    81  	before := reflect.TypeOf(x{}).Size()
    82  
    83  	type y struct {
    84  		_ nocmp
    85  		_ x
    86  	}
    87  
    88  	after := reflect.TypeOf(y{}).Size()
    89  
    90  	assert.Equal(t, before, after,
    91  		"expected nocmp to have no effect on struct size")
    92  }
    93  
    94  // This test will fail to compile if we disallow copying of nocmp.
    95  //
    96  // We need to allow this so that users can do,
    97  //
    98  //	var x atomic.Int32
    99  //	x = atomic.NewInt32(1)
   100  func TestNocmpCopy(t *testing.T) {
   101  	type foo struct{ _ nocmp }
   102  
   103  	t.Run("struct copy", func(t *testing.T) {
   104  		a := foo{}
   105  		b := a
   106  		_ = b // unused
   107  	})
   108  
   109  	t.Run("pointer copy", func(t *testing.T) {
   110  		a := &foo{}
   111  		b := *a
   112  		_ = b // unused
   113  	})
   114  }
   115  
   116  // Fake go.mod with no dependencies.
   117  const _exampleGoMod = `module example.com/nocmp`
   118  
   119  const _badFile = `package atomic
   120  
   121  import "fmt"
   122  
   123  type Int64 struct {
   124  	nocmp
   125  
   126  	v int64
   127  }
   128  
   129  func shouldNotCompile() {
   130  	var x, y Int64
   131  	fmt.Println(x == y)
   132  }
   133  `
   134  
   135  func TestNocmpIntegration(t *testing.T) {
   136  	tempdir := t.TempDir()
   137  
   138  	nocmp, err := os.ReadFile("nocmp.go")
   139  	require.NoError(t, err, "unable to read nocmp.go")
   140  
   141  	require.NoError(t,
   142  		os.WriteFile(filepath.Join(tempdir, "go.mod"), []byte(_exampleGoMod), 0o644),
   143  		"unable to write go.mod")
   144  
   145  	require.NoError(t,
   146  		os.WriteFile(filepath.Join(tempdir, "nocmp.go"), nocmp, 0o644),
   147  		"unable to write nocmp.go")
   148  
   149  	require.NoError(t,
   150  		os.WriteFile(filepath.Join(tempdir, "bad.go"), []byte(_badFile), 0o644),
   151  		"unable to write bad.go")
   152  
   153  	var stderr bytes.Buffer
   154  	cmd := exec.Command("go", "build")
   155  	cmd.Dir = tempdir
   156  	// Create a minimal build environment with only HOME set so that "go
   157  	// build" has somewhere to put the cache and other Go files in.
   158  	cmd.Env = []string{"HOME=" + filepath.Join(tempdir, "home")}
   159  	cmd.Stderr = &stderr
   160  	require.Error(t, cmd.Run(), "bad.go must not compile")
   161  
   162  	assert.Contains(t, stderr.String(),
   163  		"struct containing nocmp cannot be compared")
   164  }
   165  

View as plain text