github.com/GuanceCloud/cliutils@v1.1.21/cache/cache_test.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  package cache
     7  
     8  import (
     9  	// "os"
    10  
    11  	"strings"
    12  	"sync"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  func TestPut(t *testing.T) {
    18  	cases := []struct {
    19  		name string
    20  		data []string
    21  	}{
    22  		{
    23  			name: "basic",
    24  			data: []string{
    25  				"hello world",
    26  				"hello 1024",
    27  			},
    28  		},
    29  	}
    30  
    31  	for _, tc := range cases {
    32  		t.Run(tc.name, func(t *testing.T) {
    33  			cache, err := New("abc", 0)
    34  			if err != nil {
    35  				t.Error(err)
    36  				return
    37  			}
    38  
    39  			defer func() {
    40  				if err := cache.Close(); err != nil {
    41  					t.Error(err)
    42  				}
    43  			}()
    44  
    45  			for _, x := range tc.data {
    46  				if err := cache.Put([]byte(x)); err != nil {
    47  					t.Error(err)
    48  				}
    49  			}
    50  		})
    51  	}
    52  }
    53  
    54  func TestConcurrentPutGet(t *testing.T) {
    55  	cases := []struct {
    56  		name string
    57  		data []string
    58  	}{
    59  		{
    60  			name: "short",
    61  			data: []string{
    62  				"hello world",
    63  				"hello 1024",
    64  				"hello 2048",
    65  				"hello 3048",
    66  				"hello 4048",
    67  				"hello 5048",
    68  				"hello 6048",
    69  			},
    70  		},
    71  
    72  		{
    73  			name: "large",
    74  			data: []string{
    75  				strings.Repeat("hello world", 1000), strings.Repeat("hello 1024", 1000), strings.Repeat("hello 2048", 1000),
    76  				strings.Repeat("hello 3048", 1000), strings.Repeat("hello 4048", 1000), strings.Repeat("hello 5048", 1000),
    77  				strings.Repeat("hello 6048", 1000), strings.Repeat("hello world", 1000), strings.Repeat("hello world", 1000),
    78  				strings.Repeat("hello world", 1000), strings.Repeat("hello world", 1000), strings.Repeat("hello 1024", 1000),
    79  				strings.Repeat("hello 2048", 1000), strings.Repeat("hello 3048", 1000), strings.Repeat("hello 4048", 1000),
    80  				strings.Repeat("hello 5048", 1000), strings.Repeat("hello 6048", 1000), strings.Repeat("hello 1024", 1000),
    81  				strings.Repeat("hello 2048", 1000), strings.Repeat("hello 3048", 1000), strings.Repeat("hello 4048", 1000),
    82  				strings.Repeat("hello 5048", 1000), strings.Repeat("hello 6048", 1000), strings.Repeat("hello 1024", 1000),
    83  				strings.Repeat("hello world", 1000), strings.Repeat("hello 1024", 1000), strings.Repeat("hello 2048", 1000),
    84  				strings.Repeat("hello 3048", 1000), strings.Repeat("hello 4048", 1000), strings.Repeat("hello 5048", 1000),
    85  				strings.Repeat("hello 6048", 1000), strings.Repeat("hello world", 1000), strings.Repeat("hello world", 1000),
    86  				strings.Repeat("hello world", 1000), strings.Repeat("hello world", 1000), strings.Repeat("hello 1024", 1000),
    87  				strings.Repeat("hello 2048", 1000), strings.Repeat("hello 3048", 1000), strings.Repeat("hello 4048", 1000),
    88  				strings.Repeat("hello 5048", 1000), strings.Repeat("hello 6048", 1000), strings.Repeat("hello 1024", 1000),
    89  				strings.Repeat("hello 2048", 1000), strings.Repeat("hello 3048", 1000), strings.Repeat("hello 4048", 1000),
    90  				strings.Repeat("hello 5048", 1000), strings.Repeat("hello 6048", 1000), strings.Repeat("hello 1024", 1000),
    91  				strings.Repeat("hello 2048", 1000), strings.Repeat("hello 3048", 1000), strings.Repeat("hello 4048", 1000),
    92  				strings.Repeat("hello 5048", 1000), strings.Repeat("hello 6048", 1000), strings.Repeat("hello 1024", 1000),
    93  				strings.Repeat("hello 2048", 1000), strings.Repeat("hello 3048", 1000), strings.Repeat("hello 4048", 1000),
    94  				strings.Repeat("hello 5048", 1000), strings.Repeat("hello 6048", 1000), strings.Repeat("hello 6048", 1000),
    95  				strings.Repeat("hello 2048", 1000), strings.Repeat("hello 3048", 1000), strings.Repeat("hello 4048", 1000),
    96  				strings.Repeat("hello 5048", 1000), strings.Repeat("hello 6048", 1000), strings.Repeat("hello 1024", 1000),
    97  				strings.Repeat("hello 2048", 1000), strings.Repeat("hello 3048", 1000), strings.Repeat("hello 4048", 1000),
    98  				strings.Repeat("hello 5048", 1000), strings.Repeat("hello 6048", 1000), strings.Repeat("hello 6048", 1000),
    99  			},
   100  		},
   101  
   102  		{
   103  			name: "mixed",
   104  			data: []string{
   105  				strings.Repeat("hello world", 1000), "hello world", "hello 1024",
   106  				"hello 5048", "hello 6048", strings.Repeat("hello 1024", 1000),
   107  				strings.Repeat("hello 2048", 1000), strings.Repeat("hello 3048", 1000), "hello 2048",
   108  				"hello 3048", "hello 4048", strings.Repeat("hello 4048", 1000),
   109  				strings.Repeat("hello 5048", 1000), strings.Repeat("hello 6048", 1000),
   110  				strings.Repeat("hello world", 1000), "hello world", "hello 1024",
   111  				strings.Repeat("hello world", 1000), "hello world", "hello 1024",
   112  				strings.Repeat("hello world", 1000), "hello world", "hello 1024",
   113  				strings.Repeat("hello world", 1000), "hello world", "hello 1024",
   114  				strings.Repeat("hello world", 1000), "hello world", "hello 1024",
   115  				"hello 5048", "hello 6048", strings.Repeat("hello 1024", 1000),
   116  				strings.Repeat("hello 2048", 1000), strings.Repeat("hello 3048", 1000), "hello 2048",
   117  				"hello 3048", "hello 4048", strings.Repeat("hello 4048", 1000),
   118  				strings.Repeat("hello 5048", 1000), strings.Repeat("hello 6048", 1000),
   119  				"hello 5048", "hello 6048", strings.Repeat("hello 1024", 1000),
   120  				strings.Repeat("hello 2048", 1000), strings.Repeat("hello 3048", 1000), "hello 2048",
   121  				"hello 3048", "hello 4048", strings.Repeat("hello 4048", 1000),
   122  				strings.Repeat("hello 5048", 1000), strings.Repeat("hello 6048", 1000),
   123  				"hello 5048", "hello 6048", strings.Repeat("hello 1024", 1000),
   124  				strings.Repeat("hello 2048", 1000), strings.Repeat("hello 3048", 1000), "hello 2048",
   125  				"hello 3048", "hello 4048", strings.Repeat("hello 4048", 1000),
   126  				strings.Repeat("hello 5048", 1000), strings.Repeat("hello 6048", 1000),
   127  				"hello 5048", "hello 6048", strings.Repeat("hello 1024", 1000),
   128  				strings.Repeat("hello 2048", 1000), strings.Repeat("hello 3048", 1000), "hello 2048",
   129  				"hello 3048", "hello 4048", strings.Repeat("hello 4048", 1000),
   130  				strings.Repeat("hello 5048", 1000), strings.Repeat("hello 6048", 1000),
   131  				"hello 5048", "hello 6048", strings.Repeat("hello 1024", 1000),
   132  				strings.Repeat("hello 2048", 1000), strings.Repeat("hello 3048", 1000), "hello 2048",
   133  				"hello 3048", "hello 4048", strings.Repeat("hello 4048", 1000),
   134  				strings.Repeat("hello 5048", 1000), strings.Repeat("hello 6048", 1000),
   135  			},
   136  		},
   137  	}
   138  
   139  	for _, tc := range cases {
   140  		t.Run(tc.name, func(t *testing.T) {
   141  			var wg sync.WaitGroup
   142  
   143  			cache, err := New("abc", 0)
   144  			if err != nil {
   145  				t.Error(err)
   146  				return
   147  			}
   148  
   149  			wg.Add(len(tc.data))
   150  			for _, x := range tc.data { // multiple writer
   151  				go func(str string) {
   152  					defer wg.Done()
   153  					i := 0
   154  					for {
   155  						i++
   156  						if err := cache.Put([]byte(str)); err != nil {
   157  							t.Error(err)
   158  						}
   159  
   160  						if i > 100 {
   161  							t.Logf("Put done.")
   162  							return
   163  						}
   164  					}
   165  				}(x)
   166  			}
   167  
   168  			time.Sleep(time.Second)
   169  
   170  			wg.Add(4)
   171  			for i := 0; i < 4; i++ {
   172  				go func() { // multiple reader
   173  					defer wg.Done()
   174  					tick := time.NewTicker(time.Millisecond * 200)
   175  					defer tick.Stop()
   176  					for range tick.C {
   177  						start := time.Now()
   178  						if err := cache.Get(func(data []byte) error {
   179  							t.Logf("get data(%d bytes), cost: %s", len(data), time.Since(start))
   180  							return nil
   181  						}); err != nil {
   182  							t.Logf("Get: %s", err)
   183  							return
   184  						}
   185  					}
   186  				}()
   187  			}
   188  
   189  			wg.Wait()
   190  
   191  			if err := cache.Close(); err != nil {
   192  				t.Error(err)
   193  			}
   194  		})
   195  	}
   196  }
   197  
   198  func TestConcurrentPut(t *testing.T) {
   199  	cases := []struct {
   200  		name string
   201  		data []string
   202  	}{
   203  		{
   204  			name: "short",
   205  			data: []string{
   206  				"hello world",
   207  				"hello 1024",
   208  				"hello 2048",
   209  				"hello 3048",
   210  				"hello 4048",
   211  				"hello 5048",
   212  				"hello 6048",
   213  			},
   214  		},
   215  
   216  		{
   217  			name: "large",
   218  			data: []string{
   219  				strings.Repeat("hello world", 1000),
   220  				strings.Repeat("hello 1024", 1000),
   221  				strings.Repeat("hello 2048", 1000),
   222  				strings.Repeat("hello 3048", 1000),
   223  				strings.Repeat("hello 4048", 1000),
   224  				strings.Repeat("hello 5048", 1000),
   225  				strings.Repeat("hello 6048", 1000),
   226  			},
   227  		},
   228  	}
   229  
   230  	for _, tc := range cases {
   231  		t.Run(tc.name, func(t *testing.T) {
   232  			var wg sync.WaitGroup
   233  
   234  			cache, err := New("abc", 0)
   235  			if err != nil {
   236  				t.Error(err)
   237  				return
   238  			}
   239  
   240  			defer func() {
   241  				if err := cache.Close(); err != nil {
   242  					t.Error(err)
   243  				}
   244  			}()
   245  
   246  			wg.Add(len(tc.data))
   247  			for _, x := range tc.data {
   248  				go func(str string) {
   249  					defer wg.Done()
   250  					i := 0
   251  					for {
   252  						i++
   253  						if err := cache.Put([]byte(str)); err != nil {
   254  							t.Error(err)
   255  						}
   256  
   257  						if i > 100 {
   258  							return
   259  						}
   260  					}
   261  				}(x)
   262  			}
   263  
   264  			wg.Wait()
   265  		})
   266  	}
   267  }
   268  
   269  func TestGet(t *testing.T) {
   270  	cases := []struct {
   271  		name string
   272  		data []string
   273  	}{
   274  		{
   275  			name: "basic",
   276  			data: []string{
   277  				"hello world",
   278  				"hello 1024",
   279  				"hello 2048",
   280  			},
   281  		},
   282  	}
   283  
   284  	for _, tc := range cases {
   285  		t.Run(tc.name, func(t *testing.T) {
   286  			cache, err := New("abc", 0)
   287  			if err != nil {
   288  				t.Error(err)
   289  				return
   290  			}
   291  
   292  			defer func() {
   293  				if err := cache.Close(); err != nil {
   294  					t.Error(err)
   295  				}
   296  			}()
   297  
   298  			for _, x := range tc.data {
   299  				if err := cache.Put([]byte(x)); err != nil {
   300  					t.Error(err)
   301  				}
   302  			}
   303  
   304  			for i := 0; i < len(tc.data)-1; i++ {
   305  				if err := cache.Get(func(data []byte) error {
   306  					t.Logf("get: %s", string(data))
   307  					return nil
   308  				}); err != nil {
   309  					t.Error(err)
   310  				}
   311  			}
   312  		})
   313  	}
   314  }