Files
wehub-resource-sync 5357c39144
Fuzzer / Run Fuzzer (push) Has been cancelled
Race tests / Go race tests (ubuntu-22.04) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:01:40 +08:00

120 lines
3.5 KiB
Go

// Copyright 2026 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package memlimit
import (
"math"
"runtime/debug"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDefaults(t *testing.T) {
b := defaults()
assert.Equal(t, uint64(DefaultNodeCacheSize), b.NodeCache)
assert.Equal(t, uint64(DefaultMemtableSize), b.Memtable)
assert.Equal(t, uint64(DefaultDecodedChunksSize), b.DecodedChunks)
}
func TestComputeNoLimit(t *testing.T) {
prev := debug.SetMemoryLimit(-1)
defer debug.SetMemoryLimit(prev)
debug.SetMemoryLimit(math.MaxInt64)
b := compute()
assert.Equal(t, uint64(DefaultNodeCacheSize), b.NodeCache)
assert.Equal(t, uint64(DefaultMemtableSize), b.Memtable)
assert.Equal(t, uint64(DefaultDecodedChunksSize), b.DecodedChunks)
}
func TestComputeZeroLimit(t *testing.T) {
prev := debug.SetMemoryLimit(-1)
defer debug.SetMemoryLimit(prev)
debug.SetMemoryLimit(0)
b := compute()
assert.Equal(t, uint64(DefaultNodeCacheSize), b.NodeCache)
}
func TestCompute512MiB(t *testing.T) {
prev := debug.SetMemoryLimit(-1)
defer debug.SetMemoryLimit(prev)
limit := int64(512 * 1024 * 1024)
debug.SetMemoryLimit(limit)
b := compute()
usable := float64(limit) * 0.50
assert.Equal(t, uint64(usable*0.47), b.NodeCache)
assert.Equal(t, uint64(usable*0.47), b.Memtable)
assert.Equal(t, uint64(usable*0.06), b.DecodedChunks)
total := int64(b.NodeCache) + int64(b.Memtable) + int64(b.DecodedChunks)
assert.Less(t, total, limit, "total cache budget should be under GOMEMLIMIT")
}
func TestCompute128MiB(t *testing.T) {
prev := debug.SetMemoryLimit(-1)
defer debug.SetMemoryLimit(prev)
debug.SetMemoryLimit(int64(128 * 1024 * 1024))
b := compute()
assert.GreaterOrEqual(t, b.NodeCache, uint64(minNodeCacheSize))
assert.GreaterOrEqual(t, b.Memtable, uint64(minMemtableSize))
assert.GreaterOrEqual(t, b.DecodedChunks, uint64(minDecodedChunksSize))
assert.Less(t, b.NodeCache, uint64(DefaultNodeCacheSize))
assert.Less(t, b.Memtable, uint64(DefaultMemtableSize))
assert.Less(t, b.DecodedChunks, uint64(DefaultDecodedChunksSize))
}
func TestComputeVerySmall(t *testing.T) {
prev := debug.SetMemoryLimit(-1)
defer debug.SetMemoryLimit(prev)
debug.SetMemoryLimit(int64(32 * 1024 * 1024))
b := compute()
assert.Equal(t, uint64(minNodeCacheSize), b.NodeCache)
assert.GreaterOrEqual(t, b.Memtable, uint64(minMemtableSize))
assert.Equal(t, uint64(minDecodedChunksSize), b.DecodedChunks)
}
func TestComputeLarge(t *testing.T) {
prev := debug.SetMemoryLimit(-1)
defer debug.SetMemoryLimit(prev)
debug.SetMemoryLimit(int64(4 * 1024 * 1024 * 1024))
b := compute()
// With max caps, caches should never exceed defaults even with large GOMEMLIMIT.
assert.Equal(t, uint64(maxNodeCacheSize), b.NodeCache)
assert.Equal(t, uint64(maxMemtableSize), b.Memtable)
assert.Equal(t, uint64(maxDecodedChunksSize), b.DecodedChunks)
}
func TestAccessorsCallInit(t *testing.T) {
require.Greater(t, NodeCacheSize(), uint64(0))
require.Greater(t, MemtableSize(), uint64(0))
require.Greater(t, DecodedChunksSize(), uint64(0))
}