github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/api/query/cache/index/index_medium_test.go (about)

     1  //go:build medium
     2  // +build medium
     3  
     4  // Copyright 2018 The WPT Dashboard Project. All rights reserved.
     5  // Use of this source code is governed by a BSD-style license that can be
     6  // found in the LICENSE file.
     7  
     8  package index
     9  
    10  import (
    11  	"runtime"
    12  	"strconv"
    13  	"testing"
    14  
    15  	"github.com/golang/mock/gomock"
    16  	"github.com/stretchr/testify/assert"
    17  	"github.com/web-platform-tests/wpt.fyi/shared"
    18  	metrics "github.com/web-platform-tests/wpt.fyi/shared/metrics"
    19  )
    20  
    21  const (
    22  	testEvictionNumResults = 10000
    23  	// Each result should require at least one byte for its name + 4 bytes for
    24  	// "PASS".
    25  	testEvictionMinBytes = testEvictionNumResults * 5
    26  )
    27  
    28  func TestEvictAnyRunRelievesMemoryPressure(t *testing.T) {
    29  	ctrl := gomock.NewController(t)
    30  	defer ctrl.Finish()
    31  	loader := NewMockReportLoader(ctrl)
    32  	i, err := NewShardedWPTIndex(loader, 1)
    33  	assert.Nil(t, err)
    34  	run := shared.TestRun{
    35  		ID:            1,
    36  		RawResultsURL: "http://example.com/results.json",
    37  	}
    38  	results := make([]*metrics.TestResults, 0, testEvictionNumResults)
    39  	for j := 0; j < testEvictionNumResults; j++ {
    40  		str := strconv.Itoa(j)
    41  		results = append(results, &metrics.TestResults{
    42  			Test:   str,
    43  			Status: "PASS",
    44  		})
    45  	}
    46  	report := &metrics.TestResultsReport{Results: results}
    47  	loader.EXPECT().Load(run).Return(report, nil)
    48  
    49  	assert.Nil(t, i.IngestRun(run))
    50  	results = nil
    51  	runtime.GC()
    52  
    53  	var stats runtime.MemStats
    54  	runtime.GC()
    55  	runtime.ReadMemStats(&stats)
    56  	baseline := stats.HeapAlloc
    57  
    58  	n, err := i.EvictRuns(0.0)
    59  	assert.Nil(t, err)
    60  	assert.Equal(t, 1, n)
    61  
    62  	runtime.GC()
    63  	runtime.ReadMemStats(&stats)
    64  	assert.True(t, baseline-testEvictionMinBytes > stats.HeapAlloc)
    65  }