github.com/gojuno/afero@v1.1.1/mem/file_test.go (about)

     1  package mem
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestFileDataNameRace(t *testing.T) {
     9  	t.Parallel()
    10  	const someName = "someName"
    11  	const someOtherName = "someOtherName"
    12  	d := FileData{
    13  		name: someName,
    14  	}
    15  
    16  	if d.Name() != someName {
    17  		t.Errorf("Failed to read correct Name, was %v", d.Name())
    18  	}
    19  
    20  	ChangeFileName(&d, someOtherName)
    21  	if d.Name() != someOtherName {
    22  		t.Errorf("Failed to set Name, was %v", d.Name())
    23  	}
    24  
    25  	go func() {
    26  		ChangeFileName(&d, someName)
    27  	}()
    28  
    29  	if d.Name() != someName && d.Name() != someOtherName {
    30  		t.Errorf("Failed to read either Name, was %v", d.Name())
    31  	}
    32  }
    33  
    34  func TestFileDataModTimeRace(t *testing.T) {
    35  	t.Parallel()
    36  	someTime := time.Now()
    37  	someOtherTime := someTime.Add(1 * time.Minute)
    38  
    39  	d := FileData{
    40  		modtime: someTime,
    41  	}
    42  
    43  	s := FileInfo{
    44  		FileData: &d,
    45  	}
    46  
    47  	if s.ModTime() != someTime {
    48  		t.Errorf("Failed to read correct value, was %v", s.ModTime())
    49  	}
    50  
    51  	SetModTime(&d, someOtherTime)
    52  	if s.ModTime() != someOtherTime {
    53  		t.Errorf("Failed to set ModTime, was %v", s.ModTime())
    54  	}
    55  
    56  	go func() {
    57  		SetModTime(&d, someTime)
    58  	}()
    59  
    60  	if s.ModTime() != someTime && s.ModTime() != someOtherTime {
    61  		t.Errorf("Failed to read either modtime, was %v", s.ModTime())
    62  	}
    63  }
    64  
    65  func TestFileDataModeRace(t *testing.T) {
    66  	t.Parallel()
    67  	const someMode = 0777
    68  	const someOtherMode = 0660
    69  
    70  	d := FileData{
    71  		mode: someMode,
    72  	}
    73  
    74  	s := FileInfo{
    75  		FileData: &d,
    76  	}
    77  
    78  	if s.Mode() != someMode {
    79  		t.Errorf("Failed to read correct value, was %v", s.Mode())
    80  	}
    81  
    82  	SetMode(&d, someOtherMode)
    83  	if s.Mode() != someOtherMode {
    84  		t.Errorf("Failed to set Mode, was %v", s.Mode())
    85  	}
    86  
    87  	go func() {
    88  		SetMode(&d, someMode)
    89  	}()
    90  
    91  	if s.Mode() != someMode && s.Mode() != someOtherMode {
    92  		t.Errorf("Failed to read either mode, was %v", s.Mode())
    93  	}
    94  }
    95  
    96  func TestFileDataIsDirRace(t *testing.T) {
    97  	t.Parallel()
    98  
    99  	d := FileData{
   100  		dir: true,
   101  	}
   102  
   103  	s := FileInfo{
   104  		FileData: &d,
   105  	}
   106  
   107  	if s.IsDir() != true {
   108  		t.Errorf("Failed to read correct value, was %v", s.IsDir())
   109  	}
   110  
   111  	go func() {
   112  		s.Lock()
   113  		d.dir = false
   114  		s.Unlock()
   115  	}()
   116  
   117  	//just logging the value to trigger a read:
   118  	t.Logf("Value is %v", s.IsDir())
   119  }
   120  
   121  func TestFileDataSizeRace(t *testing.T) {
   122  	t.Parallel()
   123  
   124  	const someData = "Hello"
   125  	const someOtherDataSize = "Hello World"
   126  
   127  	d := FileData{
   128  		data: []byte(someData),
   129  		dir:  false,
   130  	}
   131  
   132  	s := FileInfo{
   133  		FileData: &d,
   134  	}
   135  
   136  	if s.Size() != int64(len(someData)) {
   137  		t.Errorf("Failed to read correct value, was %v", s.Size())
   138  	}
   139  
   140  	go func() {
   141  		s.Lock()
   142  		d.data = []byte(someOtherDataSize)
   143  		s.Unlock()
   144  	}()
   145  
   146  	//just logging the value to trigger a read:
   147  	t.Logf("Value is %v", s.Size())
   148  
   149  	//Testing the Dir size case
   150  	d.dir = true
   151  	if s.Size() != int64(42) {
   152  		t.Errorf("Failed to read correct value for dir, was %v", s.Size())
   153  	}
   154  }