<commit_before>// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package jvm

import (
	"testing"

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

func setup(t *testing.T) {
	t.Setenv("VESPA_HOME", t.TempDir())
}

func TestHeapSizeSimple(t *testing.T) {
	setup(t)
	var (
		aa = MegaBytesOfMemory(123)
		bb = MegaBytesOfMemory(234)
	)
	o := NewOptions(NewStandaloneContainer("foo"))
	assert.Equal(t, aa, o.CurMinHeapSize(aa))
	assert.Equal(t, bb, o.CurMaxHeapSize(bb))
	assert.Equal(t, 2, len(o.jvmArgs))
	assert.Equal(t, "-Xms123m", o.jvmArgs[0])
	assert.Equal(t, "-Xmx234m", o.jvmArgs[1])
}

func TestHeapSizeMulti(t *testing.T) {
	setup(t)
	var (
		aa = MegaBytesOfMemory(123)
		bb = MegaBytesOfMemory(234)
		cc = MegaBytesOfMemory(456)
		dd = MegaBytesOfMemory(567)
	)
	o := NewOptions(NewStandaloneContainer("foo"))
	assert.Equal(t, aa, o.CurMinHeapSize(aa))
	assert.Equal(t, aa, o.CurMaxHeapSize(aa))
	assert.Equal(t, 2, len(o.jvmArgs))
	o.AppendOption("-Xms234m")
	o.AppendOption("-Xmx456m")
	assert.Equal(t, 4, len(o.jvmArgs))
	assert.Equal(t, bb, o.CurMinHeapSize(aa))
	assert.Equal(t, bb, o.CurMinHeapSize(dd))
	assert.Equal(t, cc, o.CurMaxHeapSize(aa))
	assert.Equal(t, cc, o.CurMaxHeapSize(dd))
	o.AppendOption("-Xms1g")
	o.AppendOption("-Xmx2g")
	assert.Equal(t, GigaBytesOfMemory(1), o.CurMinHeapSize(aa))
	assert.Equal(t, GigaBytesOfMemory(2), o.CurMaxHeapSize(aa))
	o.AppendOption("-Xms16777216k")
	o.AppendOption("-Xmx32505856k")
	assert.Equal(t, KiloBytesOfMemory(16777216), o.CurMinHeapSize(aa))
	assert.Equal(t, KiloBytesOfMemory(32505856), o.CurMaxHeapSize(aa))
}

func TestHeapSizeAdd(t *testing.T) {
	setup(t)
	var (
		gg = MegaBytesOfMemory(12345)
		hh = MegaBytesOfMemory(23456)
	)
	o := NewOptions(NewStandaloneContainer("foo"))
	o.AddDefaultHeapSizeArgs(gg, hh)
	assert.Equal(t, 3, len(o.jvmArgs))
	assert.Equal(t, "-Xms12345m", o.jvmArgs[0])
	assert.Equal(t, "-Xmx23456m", o.jvmArgs[1])
	assert.Equal(t, "-XX:+UseTransparentHugePages", o.jvmArgs[2])
}

func TestHeapSizeNoAdd(t *testing.T) {
	setup(t)
	var (
		bb = MegaBytesOfMemory(234)
		cc = MegaBytesOfMemory(456)
	)
	o := NewOptions(NewStandaloneContainer("foo"))
	o.AppendOption("-Xms128k")
	o.AppendOption("-Xmx1280k")
	o.AddDefaultHeapSizeArgs(bb, cc)
	assert.Equal(t, 2, len(o.jvmArgs))
	assert.Equal(t, "-Xms128k", o.jvmArgs[0])
	assert.Equal(t, "-Xmx1280k", o.jvmArgs[1])
}<commit_msg>use a dummy Container to avoid entanglement<commit_after>// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package jvm

import (
	"testing"

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

type dummyContainer struct{ containerBase }

func (*dummyContainer) ArgForMain() string { return "arg-for-main" }
func newDummyContainer() Container {
	var dc dummyContainer
	dc.serviceName = "foo"
	dc.jvmArgs = NewOptions(&dc)
	return &dc
}

func TestHeapSizeSimple(t *testing.T) {
	var (
		aa = MegaBytesOfMemory(123)
		bb = MegaBytesOfMemory(234)
	)
	o := newDummyContainer().JvmOptions()
	assert.Equal(t, aa, o.CurMinHeapSize(aa))
	assert.Equal(t, bb, o.CurMaxHeapSize(bb))
	assert.Equal(t, 2, len(o.jvmArgs))
	assert.Equal(t, "-Xms123m", o.jvmArgs[0])
	assert.Equal(t, "-Xmx234m", o.jvmArgs[1])
}

func TestHeapSizeMulti(t *testing.T) {
	var (
		aa = MegaBytesOfMemory(123)
		bb = MegaBytesOfMemory(234)
		cc = MegaBytesOfMemory(456)
		dd = MegaBytesOfMemory(567)
	)
	o := newDummyContainer().JvmOptions()
	assert.Equal(t, aa, o.CurMinHeapSize(aa))
	assert.Equal(t, aa, o.CurMaxHeapSize(aa))
	assert.Equal(t, 2, len(o.jvmArgs))
	o.AppendOption("-Xms234m")
	o.AppendOption("-Xmx456m")
	assert.Equal(t, 4, len(o.jvmArgs))
	assert.Equal(t, bb, o.CurMinHeapSize(aa))
	assert.Equal(t, bb, o.CurMinHeapSize(dd))
	assert.Equal(t, cc, o.CurMaxHeapSize(aa))
	assert.Equal(t, cc, o.CurMaxHeapSize(dd))
	o.AppendOption("-Xms1g")
	o.AppendOption("-Xmx2g")
	assert.Equal(t, GigaBytesOfMemory(1), o.CurMinHeapSize(aa))
	assert.Equal(t, GigaBytesOfMemory(2), o.CurMaxHeapSize(aa))
	o.AppendOption("-Xms16777216k")
	o.AppendOption("-Xmx32505856k")
	assert.Equal(t, KiloBytesOfMemory(16777216), o.CurMinHeapSize(aa))
	assert.Equal(t, KiloBytesOfMemory(32505856), o.CurMaxHeapSize(aa))
}

func TestHeapSizeAdd(t *testing.T) {
	var (
		gg = MegaBytesOfMemory(12345)
		hh = MegaBytesOfMemory(23456)
	)
	o := newDummyContainer().JvmOptions()
	o.AddDefaultHeapSizeArgs(gg, hh)
	assert.Equal(t, 3, len(o.jvmArgs))
	assert.Equal(t, "-Xms12345m", o.jvmArgs[0])
	assert.Equal(t, "-Xmx23456m", o.jvmArgs[1])
	assert.Equal(t, "-XX:+UseTransparentHugePages", o.jvmArgs[2])
}

func TestHeapSizeNoAdd(t *testing.T) {
	var (
		bb = MegaBytesOfMemory(234)
		cc = MegaBytesOfMemory(456)
	)
	o := newDummyContainer().JvmOptions()
	o.AppendOption("-Xms128k")
	o.AppendOption("-Xmx1280k")
	o.AddDefaultHeapSizeArgs(bb, cc)
	assert.Equal(t, 2, len(o.jvmArgs))
	assert.Equal(t, "-Xms128k", o.jvmArgs[0])
	assert.Equal(t, "-Xmx1280k", o.jvmArgs[1])
}
{'fraction_non_alphanumeric': 0.16686771210293527, 'fraction_numerical': 0.04825090470446321, 'mean_word_length': 5.580687830687831, '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': '5753597', 'n_tokens_mistral': 2262, 'n_tokens_neox': 2120, 'n_words': 339}