github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/cache/singleflight_test.go (about)

     1  // The package is migrated from beego, you can get from following link:
     2  // import(
     3  //   "github.com/beego/beego/v2/client/cache"
     4  // )
     5  // Copyright 2023. All Rights Reserved.
     6  //
     7  // Licensed under the Apache License, Version 2.0 (the "License");
     8  // you may not use this file except in compliance with the License.
     9  // You may obtain a copy of the License at
    10  //
    11  //      http://www.apache.org/licenses/LICENSE-2.0
    12  //
    13  // Unless required by applicable law or agreed to in writing, software
    14  // distributed under the License is distributed on an "AS IS" BASIS,
    15  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  // See the License for the specific language governing permissions and
    17  // limitations under the License.
    18  
    19  package cache
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"sync"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestSingleflight_Memory_Get(t *testing.T) {
    32  	bm, err := NewCache("memory", `{"interval":20}`)
    33  	assert.Nil(t, err)
    34  
    35  	testSingleflightCacheConcurrencyGet(t, bm)
    36  }
    37  
    38  func TestSingleflight_file_Get(t *testing.T) {
    39  	fc := NewFileCache().(*FileCache)
    40  	fc.CachePath = "////aaa"
    41  	err := fc.Init()
    42  	assert.NotNil(t, err)
    43  	fc.CachePath = getTestCacheFilePath()
    44  	err = fc.Init()
    45  	assert.Nil(t, err)
    46  
    47  	testSingleflightCacheConcurrencyGet(t, fc)
    48  }
    49  
    50  func testSingleflightCacheConcurrencyGet(t *testing.T, bm Cache) {
    51  	key, value := "key3", "value3"
    52  	db := &MockOrm{keysMap: map[string]int{key: 1}, kvs: map[string]any{key: value}}
    53  	c, err := NewSingleflightCache(bm, 10*time.Second,
    54  		func(ctx context.Context, key string) (any, error) {
    55  			val, er := db.Load(key)
    56  			if er != nil {
    57  				return nil, er
    58  			}
    59  			return val, nil
    60  		})
    61  	assert.Nil(t, err)
    62  
    63  	var wg sync.WaitGroup
    64  	wg.Add(10)
    65  	for i := 0; i < 10; i++ {
    66  		go func() {
    67  			defer wg.Done()
    68  			val, err := c.Get(context.Background(), key)
    69  			if err != nil {
    70  				t.Error(err)
    71  			}
    72  			assert.Equal(t, value, val)
    73  		}()
    74  		time.Sleep(1 * time.Millisecond)
    75  	}
    76  	wg.Wait()
    77  }
    78  
    79  func ExampleNewSingleflightCache() {
    80  	c := NewMemoryCache()
    81  	c, err := NewSingleflightCache(c, time.Minute, func(ctx context.Context, key string) (any, error) {
    82  		return fmt.Sprintf("hello, %s", key), nil
    83  	})
    84  	if err != nil {
    85  		panic(err)
    86  	}
    87  	val, err := c.Get(context.Background(), "Beego")
    88  	if err != nil {
    89  		panic(err)
    90  	}
    91  	fmt.Print(val)
    92  	// Output:
    93  	// hello, Beego
    94  }