Sybren A. Stüvel e48fa4cc5f Manager: load job compiler scripts on demand, instead of at startup
The Manager now loads the JavaScript files for job types on demand,
instead of caching them in memory at startup.

This will make certain calls a bit less performant, but in practice this
is around the order of a millisecond so it shouldn't matter much.

Fixes: #104349
2025-02-10 12:07:55 +01:00

83 lines
1.9 KiB
Go

package job_compilers
// SPDX-License-Identifier: GPL-3.0-or-later
import (
"os"
"testing"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoadScriptsFrom_skip_nonjs(t *testing.T) {
thisDirFS := os.DirFS(".")
compilers, err := loadScriptsFrom(thisDirFS)
require.NoError(t, err, "input without JS files should not cause errors")
assert.Empty(t, compilers)
}
func TestLoadScriptsFrom_on_disk_js(t *testing.T) {
scriptsFS := os.DirFS("scripts-for-unittest")
compilers, err := loadScriptsFrom(scriptsFS)
require.NoError(t, err)
expectKeys := map[string]bool{
"echo-and-sleep": true,
"simple-blender-render": true,
// Should NOT contain an entry for 'empty.js'.
}
assert.Equal(t, expectKeys, keys(compilers))
}
func TestLoadScriptsFrom_embedded(t *testing.T) {
initEmbeddedFS()
compilers, err := loadScriptsFrom(embeddedScriptsFS)
require.NoError(t, err)
expectKeys := map[string]bool{
"echo-sleep-test": true,
"simple-blender-render": true,
}
assert.Equal(t, expectKeys, keys(compilers))
}
func BenchmarkLoadScripts_fromEmbedded(b *testing.B) {
zerolog.SetGlobalLevel(zerolog.Disabled)
initEmbeddedFS()
for i := 0; i < b.N; i++ {
compilers, err := loadScriptsFrom(embeddedScriptsFS)
require.NoError(b, err)
assert.NotEmpty(b, compilers)
}
}
func BenchmarkLoadScripts_fromDisk(b *testing.B) {
zerolog.SetGlobalLevel(zerolog.Disabled)
onDiskFS := os.DirFS("scripts-for-unittest")
for i := 0; i < b.N; i++ {
compilers, err := loadScriptsFrom(onDiskFS)
require.NoError(b, err)
assert.NotEmpty(b, compilers)
}
}
func BenchmarkLoadScripts(b *testing.B) {
zerolog.SetGlobalLevel(zerolog.Disabled)
for i := 0; i < b.N; i++ {
loadScripts()
}
}
// keys returns the set of keys of the mapping.
func keys[K comparable, V any](mapping map[K]V) map[K]bool {
keys := map[K]bool{}
for k := range mapping {
keys[k] = true
}
return keys
}