<commit_before>// +build integration

package memory

import (
	"context"
	"runtime"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestMemoryAllocationAttack(t *testing.T) {
	assert := assert.New(t)
	var size uint64 = 200 * MiB

	ma, err := NewMemAllocation(size)
	assert.NoError(err, "Creation of memory allocator shouldn't error")

	// Get current memory
	var mem runtime.MemStats
	runtime.ReadMemStats(&mem)
	startMem := mem.Alloc

	// Allocate memory and test if increased.
	ma.Apply(context.TODO())
	time.Sleep(1 * time.Millisecond)
	runtime.ReadMemStats(&mem)
	endMem := mem.Alloc

	// Let 10% margin delta from the wanted size
	assert.InDelta((endMem - startMem), size, float64(size)*0.15, "current memory allocation should be wanted allocation (15% deviation)")
	// Free memory and test if released.
	ma.Revert()
	time.Sleep(1 * time.Millisecond)
	runtime.ReadMemStats(&mem)

	// Let 10% margin delta from the wanted size
	assert.InDelta(startMem, mem.Alloc, float64(size)*0.15, "current memory and initial memory should be equal (15% deviation)")
}<commit_msg>Increase delta on memory attack integration test<commit_after>// +build integration

package memory

import (
	"context"
	"runtime"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestMemoryAllocationAttack(t *testing.T) {
	assert := assert.New(t)
	var size uint64 = 200 * MiB

	ma, err := NewMemAllocation(size)
	assert.NoError(err, "Creation of memory allocator shouldn't error")

	// Get current memory
	var mem runtime.MemStats
	runtime.ReadMemStats(&mem)
	startMem := mem.Alloc

	// Allocate memory and test if increased.
	ma.Apply(context.TODO())
	time.Sleep(1 * time.Millisecond)
	runtime.ReadMemStats(&mem)
	endMem := mem.Alloc

	// Let 10% margin delta from the wanted size
	assert.InDelta((endMem - startMem), size, float64(size)*0.35, "current memory allocation should be wanted allocation (35% deviation)")
	// Free memory and test if released.
	ma.Revert()
	time.Sleep(1 * time.Millisecond)
	runtime.ReadMemStats(&mem)

	// Let 10% margin delta from the wanted size
	assert.InDelta(startMem, mem.Alloc, float64(size)*0.35, "current memory and initial memory should be equal (35% deviation)")
}
{'fraction_non_alphanumeric': 0.1201988251242657, 'fraction_numerical': 0.022593764121102575, 'mean_word_length': 4.548872180451128, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3615993', 'n_tokens_mistral': 769, 'n_tokens_neox': 718, 'n_words': 214}