github.com/TeaOSLab/EdgeNode@v1.3.8/internal/caches/manager_test.go (about)

     1  package caches_test
     2  
     3  import (
     4  	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
     5  	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
     6  	"github.com/TeaOSLab/EdgeNode/internal/caches"
     7  	"github.com/iwind/TeaGo/Tea"
     8  	"testing"
     9  )
    10  
    11  func TestManager_UpdatePolicies(t *testing.T) {
    12  	{
    13  		var policies = []*serverconfigs.HTTPCachePolicy{}
    14  		caches.SharedManager.UpdatePolicies(policies)
    15  		printManager(t)
    16  	}
    17  
    18  	{
    19  		var policies = []*serverconfigs.HTTPCachePolicy{
    20  			{
    21  				Id:   1,
    22  				Type: serverconfigs.CachePolicyStorageFile,
    23  				Options: map[string]interface{}{
    24  					"dir": Tea.Root + "/caches",
    25  				},
    26  			},
    27  			{
    28  				Id:   2,
    29  				Type: serverconfigs.CachePolicyStorageFile,
    30  				Options: map[string]interface{}{
    31  					"dir": Tea.Root + "/caches",
    32  				},
    33  			},
    34  			{
    35  				Id:   3,
    36  				Type: serverconfigs.CachePolicyStorageFile,
    37  				Options: map[string]interface{}{
    38  					"dir": Tea.Root + "/caches",
    39  				},
    40  			},
    41  		}
    42  		caches.SharedManager.UpdatePolicies(policies)
    43  		printManager(t)
    44  	}
    45  
    46  	{
    47  		var policies = []*serverconfigs.HTTPCachePolicy{
    48  			{
    49  				Id:   1,
    50  				Type: serverconfigs.CachePolicyStorageFile,
    51  				Options: map[string]interface{}{
    52  					"dir": Tea.Root + "/caches",
    53  				},
    54  			},
    55  			{
    56  				Id:   2,
    57  				Type: serverconfigs.CachePolicyStorageFile,
    58  				Options: map[string]interface{}{
    59  					"dir": Tea.Root + "/caches",
    60  				},
    61  			},
    62  			{
    63  				Id:   4,
    64  				Type: serverconfigs.CachePolicyStorageFile,
    65  				Options: map[string]interface{}{
    66  					"dir": Tea.Root + "/caches",
    67  				},
    68  			},
    69  		}
    70  		caches.SharedManager.UpdatePolicies(policies)
    71  		printManager(t)
    72  	}
    73  }
    74  
    75  func TestManager_ChangePolicy_Memory(t *testing.T) {
    76  	var policies = []*serverconfigs.HTTPCachePolicy{
    77  		{
    78  			Id:       1,
    79  			Type:     serverconfigs.CachePolicyStorageMemory,
    80  			Options:  map[string]interface{}{},
    81  			Capacity: &shared.SizeCapacity{Count: 1, Unit: shared.SizeCapacityUnitGB},
    82  		},
    83  	}
    84  	caches.SharedManager.UpdatePolicies(policies)
    85  	caches.SharedManager.UpdatePolicies([]*serverconfigs.HTTPCachePolicy{
    86  		{
    87  			Id:       1,
    88  			Type:     serverconfigs.CachePolicyStorageMemory,
    89  			Options:  map[string]interface{}{},
    90  			Capacity: &shared.SizeCapacity{Count: 2, Unit: shared.SizeCapacityUnitGB},
    91  		},
    92  	})
    93  }
    94  
    95  func TestManager_ChangePolicy_File(t *testing.T) {
    96  	var policies = []*serverconfigs.HTTPCachePolicy{
    97  		{
    98  			Id:   1,
    99  			Type: serverconfigs.CachePolicyStorageFile,
   100  			Options: map[string]interface{}{
   101  				"dir": Tea.Root + "/data/cache-index/p1",
   102  			},
   103  			Capacity: &shared.SizeCapacity{Count: 1, Unit: shared.SizeCapacityUnitGB},
   104  		},
   105  	}
   106  	caches.SharedManager.UpdatePolicies(policies)
   107  	caches.SharedManager.UpdatePolicies([]*serverconfigs.HTTPCachePolicy{
   108  		{
   109  			Id:   1,
   110  			Type: serverconfigs.CachePolicyStorageFile,
   111  			Options: map[string]interface{}{
   112  				"dir": Tea.Root + "/data/cache-index/p1",
   113  			},
   114  			Capacity: &shared.SizeCapacity{Count: 2, Unit: shared.SizeCapacityUnitGB},
   115  		},
   116  	})
   117  }
   118  
   119  func TestManager_MaxSystemMemoryBytesPerStorage(t *testing.T) {
   120  	for i := 0; i < 100; i++ {
   121  		caches.SharedManager.CountMemoryStorages = i
   122  		t.Log(i, caches.SharedManager.MaxSystemMemoryBytesPerStorage()>>30, "GB")
   123  	}
   124  }
   125  
   126  func printManager(t *testing.T) {
   127  	t.Log("===manager==")
   128  	t.Log("storage:")
   129  	for _, storage := range caches.SharedManager.StorageMap() {
   130  		t.Log("  storage:", storage.Policy().Id)
   131  	}
   132  	t.Log("===============")
   133  }