github.com/m3db/m3@v1.5.0/src/x/mmap/mmap_test.go (about)

     1  // Copyright (c) 2017 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 mmap
    22  
    23  import (
    24  	"errors"
    25  	"io/ioutil"
    26  	"os"
    27  	"testing"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  type mmapFdFuncType func(fd, offset, length int64, opts Options) (Descriptor, error)
    33  
    34  func TestMmapFile(t *testing.T) {
    35  	fd, err := ioutil.TempFile("", "testfile")
    36  	assert.NoError(t, err)
    37  
    38  	desc, err := File(fd, Options{})
    39  	assert.NoError(t, err)
    40  	assert.NoError(t, desc.Warning)
    41  	assert.Equal(t, []byte{}, desc.Bytes)
    42  
    43  	Munmap(desc)
    44  }
    45  
    46  func TestMmapFiles(t *testing.T) {
    47  	fd1, err := ioutil.TempFile("", "1")
    48  	assert.NoError(t, err)
    49  	fd1Path := fd1.Name()
    50  	fd2, err := ioutil.TempFile("", "2")
    51  	assert.NoError(t, err)
    52  	fd2Path := fd2.Name()
    53  
    54  	var (
    55  		desc1 = Descriptor{}
    56  		desc2 = Descriptor{}
    57  	)
    58  	result, err := Files(os.Open, map[string]FileDesc{
    59  		fd1Path: FileDesc{
    60  			File:       &fd1,
    61  			Descriptor: &desc1,
    62  			Options:    Options{},
    63  		},
    64  		fd2Path: FileDesc{
    65  			File:       &fd2,
    66  			Descriptor: &desc2,
    67  			Options:    Options{},
    68  		},
    69  	})
    70  
    71  	assert.NoError(t, err)
    72  	assert.NoError(t, result.Warning)
    73  }
    74  
    75  func TestMmapFilesHandlesError(t *testing.T) {
    76  	fd1, err := ioutil.TempFile("", "1")
    77  	assert.NoError(t, err)
    78  	fd1Path := fd1.Name()
    79  
    80  	fd2, err := ioutil.TempFile("", "doesnt-matter")
    81  	assert.NoError(t, err)
    82  	var (
    83  		desc1 = Descriptor{}
    84  		desc2 = Descriptor{}
    85  	)
    86  	_, err = Files(os.Open, map[string]FileDesc{
    87  		fd1Path: FileDesc{
    88  			File:       &fd1,
    89  			Descriptor: &desc1,
    90  			Options:    Options{},
    91  		},
    92  		"does_not_exist": FileDesc{
    93  			File:       &fd2,
    94  			Descriptor: &desc2,
    95  			Options:    Options{},
    96  		},
    97  	})
    98  
    99  	assert.Error(t, err)
   100  }
   101  
   102  func TestMmapFilesHandlesWarnings(t *testing.T) {
   103  	mmapFdReturnWarn := func(fd, offset, length int64, opts Options) (Descriptor, error) {
   104  		return Descriptor{Warning: errors.New("some-error"), Bytes: []byte("a")}, nil
   105  	}
   106  	defer mockMmapFdFunc(mmapFdReturnWarn)()
   107  
   108  	fd1, err := ioutil.TempFile("", "1")
   109  	assert.NoError(t, err)
   110  	fd1Path := fd1.Name()
   111  
   112  	desc1 := Descriptor{}
   113  
   114  	result, err := Files(os.Open, map[string]FileDesc{
   115  		fd1Path: FileDesc{
   116  			File:       &fd1,
   117  			Descriptor: &desc1,
   118  			Options:    Options{},
   119  		},
   120  	})
   121  
   122  	assert.NoError(t, err)
   123  	// Warning should be present AND byte slice pointer should have been
   124  	// modified as well
   125  	assert.Error(t, result.Warning)
   126  	assert.Equal(t, []byte("a"), desc1.Bytes)
   127  }
   128  
   129  func mockMmapFdFunc(f mmapFdFuncType) func() {
   130  	old := mmapFdFn
   131  	mmapFdFn = f
   132  	return func() {
   133  		mmapFdFn = old
   134  	}
   135  }