github.com/Ptt-official-app/go-bbs@v0.12.0/cache/cache_test.go (about)

     1  // +build darwin linux unix
     2  
     3  package cache
     4  
     5  import (
     6  	"testing"
     7  )
     8  
     9  func TestNewCacheWithMmap(t *testing.T) {
    10  
    11  	data, err := CreateMmap("./test", 20)
    12  	if err != nil {
    13  		t.Logf("err should be nil, got: %v", err)
    14  	}
    15  
    16  	data.Bytes()[0] = 42
    17  
    18  	data.Close()
    19  
    20  	cache, err := NewCache("file:./test")
    21  	if err != nil {
    22  		t.Logf("err should be nil, got: %v", err)
    23  	}
    24  
    25  	if cache.Bytes()[0] != 42 {
    26  		t.Errorf("cache buf should be %v, got %v", 42, cache.Bytes()[0])
    27  	}
    28  	cache.Bytes()[0] = 43
    29  
    30  	err = cache.Close()
    31  	if err != nil {
    32  		t.Logf("err should be nil, got: %v", err)
    33  	}
    34  
    35  	RemoveMmap("./test")
    36  }
    37  
    38  func TestNewCacheWithSHM(t *testing.T) {
    39  
    40  	data, err := CreateKey(10, 4)
    41  	data.Bytes()[0] = 42
    42  	if err != nil {
    43  		t.Logf("err should be nil, got: %v", err)
    44  	}
    45  
    46  	if data.Bytes()[0] != 42 {
    47  		t.Errorf("data buf should be %v, got %v", 42, data.Bytes()[0])
    48  	}
    49  	data.Close()
    50  
    51  	cache, err := NewCache("shmkey:10")
    52  	if err != nil {
    53  		t.Logf("err should be nil, got: %v", err)
    54  	}
    55  
    56  	if len(cache.Bytes()) != 4 {
    57  		t.Errorf("cache buf length not correct, expected 4, got: %v", len(cache.Bytes()))
    58  	}
    59  
    60  	if cache.Bytes()[0] != 42 {
    61  		t.Errorf("cache buf should be %v, got %v", 42, cache.Bytes()[0])
    62  	}
    63  	cache.Bytes()[0] = 43
    64  
    65  	err = cache.Close()
    66  
    67  	RemoveKey(10)
    68  }