github.com/lyeb/hugo@v0.47.1/cache/partitioned_lazy_cache_test.go (about)

     1  // Copyright 2017-present The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package cache
    15  
    16  import (
    17  	"errors"
    18  	"sync"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestNewPartitionedLazyCache(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	assert := require.New(t)
    28  
    29  	p1 := Partition{
    30  		Key: "p1",
    31  		Load: func() (map[string]interface{}, error) {
    32  			return map[string]interface{}{
    33  				"p1_1":   "p1v1",
    34  				"p1_2":   "p1v2",
    35  				"p1_nil": nil,
    36  			}, nil
    37  		},
    38  	}
    39  
    40  	p2 := Partition{
    41  		Key: "p2",
    42  		Load: func() (map[string]interface{}, error) {
    43  			return map[string]interface{}{
    44  				"p2_1": "p2v1",
    45  				"p2_2": "p2v2",
    46  				"p2_3": "p2v3",
    47  			}, nil
    48  		},
    49  	}
    50  
    51  	cache := NewPartitionedLazyCache(p1, p2)
    52  
    53  	v, err := cache.Get("p1", "p1_1")
    54  	assert.NoError(err)
    55  	assert.Equal("p1v1", v)
    56  
    57  	v, err = cache.Get("p1", "p2_1")
    58  	assert.NoError(err)
    59  	assert.Nil(v)
    60  
    61  	v, err = cache.Get("p1", "p1_nil")
    62  	assert.NoError(err)
    63  	assert.Nil(v)
    64  
    65  	v, err = cache.Get("p2", "p2_3")
    66  	assert.NoError(err)
    67  	assert.Equal("p2v3", v)
    68  
    69  	v, err = cache.Get("doesnotexist", "p1_1")
    70  	assert.NoError(err)
    71  	assert.Nil(v)
    72  
    73  	v, err = cache.Get("p1", "doesnotexist")
    74  	assert.NoError(err)
    75  	assert.Nil(v)
    76  
    77  	errorP := Partition{
    78  		Key: "p3",
    79  		Load: func() (map[string]interface{}, error) {
    80  			return nil, errors.New("Failed")
    81  		},
    82  	}
    83  
    84  	cache = NewPartitionedLazyCache(errorP)
    85  
    86  	v, err = cache.Get("p1", "doesnotexist")
    87  	assert.NoError(err)
    88  	assert.Nil(v)
    89  
    90  	_, err = cache.Get("p3", "doesnotexist")
    91  	assert.Error(err)
    92  
    93  }
    94  
    95  func TestConcurrentPartitionedLazyCache(t *testing.T) {
    96  	t.Parallel()
    97  
    98  	assert := require.New(t)
    99  
   100  	var wg sync.WaitGroup
   101  
   102  	p1 := Partition{
   103  		Key: "p1",
   104  		Load: func() (map[string]interface{}, error) {
   105  			return map[string]interface{}{
   106  				"p1_1":   "p1v1",
   107  				"p1_2":   "p1v2",
   108  				"p1_nil": nil,
   109  			}, nil
   110  		},
   111  	}
   112  
   113  	p2 := Partition{
   114  		Key: "p2",
   115  		Load: func() (map[string]interface{}, error) {
   116  			return map[string]interface{}{
   117  				"p2_1": "p2v1",
   118  				"p2_2": "p2v2",
   119  				"p2_3": "p2v3",
   120  			}, nil
   121  		},
   122  	}
   123  
   124  	cache := NewPartitionedLazyCache(p1, p2)
   125  
   126  	for i := 0; i < 100; i++ {
   127  		wg.Add(1)
   128  		go func() {
   129  			defer wg.Done()
   130  			for j := 0; j < 10; j++ {
   131  				v, err := cache.Get("p1", "p1_1")
   132  				assert.NoError(err)
   133  				assert.Equal("p1v1", v)
   134  			}
   135  		}()
   136  	}
   137  	wg.Wait()
   138  }