<commit_before>package gateway

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"os/signal"
	"strings"
	"testing"
	"time"

	log "github.com/Sirupsen/logrus"
	"github.com/sahib/brig/catfs"
	"github.com/sahib/brig/defaults"
	"github.com/sahib/config"
	"github.com/stretchr/testify/require"
)

func withBasicGateway(t *testing.T, fn func(gw *Gateway, fs *catfs.FS)) {
	os.RemoveAll("/tmp/test.db")

	cfg, err := config.Open(nil, defaults.Defaults, config.StrictnessPanic)
	require.Nil(t, err)

	fs, err := catfs.NewFilesystem(
		catfs.NewMemFsBackend(),
		"/tmp/test.db",
		"ali",
		false,
		cfg.Section("fs"),
	)

	require.Nil(t, err)

	cfg.SetBool("gateway.enabled", true)
	cfg.SetInt("gateway.port", 9999)
	gw := NewGateway(fs, cfg.Section("gateway"))
	gw.Start()

	fn(gw, fs)

	require.Nil(t, gw.Stop())
}

func buildURL(gw *Gateway, suffix string) string {
	suffix = strings.TrimLeft(suffix, "/")
	return fmt.Sprintf("http://localhost:%d/%s", gw.cfg.Int("port"), suffix)
}

func ping(t *testing.T, gw *Gateway) bool {
	_, err := http.Get(buildURL(gw, ""))
	return err == nil
}

func query(t *testing.T, gw *Gateway, suffix string) (int, []byte) {
	resp, err := http.Get(buildURL(gw, suffix))
	require.Nil(t, err, fmt.Sprintf("%v", err))

	data, err := ioutil.ReadAll(resp.Body)
	require.Nil(t, err, fmt.Sprintf("%v", err))
<commit_msg>gateway: try to stabilize test<commit_after>package gateway

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"os/signal"
	"strings"
	"testing"
	"time"

	log "github.com/Sirupsen/logrus"
	"github.com/sahib/brig/catfs"
	"github.com/sahib/brig/defaults"
	"github.com/sahib/config"
	"github.com/stretchr/testify/require"
)

func withBasicGateway(t *testing.T, fn func(gw *Gateway, fs *catfs.FS)) {
	os.RemoveAll("/tmp/test.db")

	cfg, err := config.Open(nil, defaults.Defaults, config.StrictnessPanic)
	require.Nil(t, err)

	fs, err := catfs.NewFilesystem(
		catfs.NewMemFsBackend(),
		"/tmp/test.db",
		"ali",
		false,
		cfg.Section("fs"),
	)

	require.Nil(t, err)

	cfg.SetBool("gateway.enabled", true)
	cfg.SetInt("gateway.port", 9999)
	gw := NewGateway(fs, cfg.Section("gateway"))
	gw.Start()

	defer func() {
		require.Nil(t, gw.Stop())
	}()

	time.Sleep(100 * time.Millisecond)
	fn(gw, fs)
}

func buildURL(gw *Gateway, suffix string) string {
	suffix = strings.TrimLeft(suffix, "/")
	return fmt.Sprintf("http://localhost:%d/%s", gw.cfg.Int("port"), suffix)
}

func ping(t *testing.T, gw *Gateway) bool {
	_, err := http.Get(buildURL(gw, ""))
	return err == nil
}

func query(t *testing.T, gw *Gateway, suffix string) (int, []byte) {
	resp, err := http.Get(buildURL(gw, suffix))
	require.Nil(t, err, fmt.Sprintf("%v", err))

	data, err := ioutil.ReadAll(resp.Body)
	require.Nil(t, err, fmt.Sprintf("%v", err))

{'fraction_non_alphanumeric': 0.19515720997470185, 'fraction_numerical': 0.003975424647632815, 'mean_word_length': 4.448818897637795, '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': '5249186', 'n_tokens_mistral': 1199, 'n_tokens_neox': 1161, 'n_words': 214}