<commit_before>package tokenserver

import (
	"context"
	"net/http"
	"testing"
	"time"

	"github.com/antihax/evedata/internal/tokenstore"
	"github.com/golang/protobuf/ptypes"
	"google.golang.org/grpc"

	"github.com/antihax/evedata/internal/redigohelper"
	"github.com/antihax/evedata/internal/sqlhelper"
	"github.com/antihax/goesi"
)

func TestTokenServer(t *testing.T) {
	sql := sqlhelper.NewTestDatabase()

	redis := redigohelper.ConnectRedisTestPool()
	redConn := redis.Get()
	defer redConn.Close()
	redConn.Do("FLUSHALL")

	// Setup an authenticator
	auth := goesi.NewSSOAuthenticator(&http.Client{}, "FAAAK", "SOFAAAAAAAKE", "",
		[]string{"esi-universe.read_structures.v1",
			"esi-search.search_structures.v1",
			"esi-markets.structure_markets.v1"})
	auth.ChangeTokenURL("http://127.0.0.1:8080")
	auth.ChangeAuthURL("http://127.0.0.1:8080")

	ts := NewTokenServer(redis, sql, auth)
	go ts.Run()

	conn, err := grpc.Dial("localhost:4001", grpc.WithInsecure())
	if err != nil {
		t.Fatal(err)
	}
	defer conn.Close()

	expire, _ := ptypes.TimestampProto(time.Now().Add(time.Hour))

	client := tokenstore.NewTokenStoreClient(conn)
	resp, err := client.SetToken(context.TODO(), &tokenstore.SetTokenRequest{
		CharacterID:      1,
		TokenCharacterID: 1,
		AccessToken:      "<KEY>",
		RefreshToken:     "<PASSWORD>",
		TokenType:        "Bearer",
		Expiry:           expire,
	})
	if err != nil {
		t.Fatal(err)<commit_msg>fix race on connection<commit_after>package tokenserver

import (
	"context"
	"net/http"
	"testing"
	"time"

	"github.com/antihax/evedata/internal/tokenstore"
	"github.com/golang/protobuf/ptypes"
	"google.golang.org/grpc"

	"github.com/antihax/evedata/internal/redigohelper"
	"github.com/antihax/evedata/internal/sqlhelper"
	"github.com/antihax/goesi"
)

func TestTokenServer(t *testing.T) {
	sql := sqlhelper.NewTestDatabase()

	redis := redigohelper.ConnectRedisTestPool()
	redConn := redis.Get()
	defer redConn.Close()
	redConn.Do("FLUSHALL")

	// Setup an authenticator
	auth := goesi.NewSSOAuthenticator(&http.Client{}, "FAAAK", "SOFAAAAAAAKE", "",
		[]string{"esi-universe.read_structures.v1",
			"esi-search.search_structures.v1",
			"esi-markets.structure_markets.v1"})
	auth.ChangeTokenURL("http://127.0.0.1:8080")
	auth.ChangeAuthURL("http://127.0.0.1:8080")

	ts := NewTokenServer(redis, sql, auth)
	go ts.Run()

	// sleep to allow server to start
	time.Sleep(time.Millisecond * 50)

	conn, err := grpc.Dial("localhost:4001", grpc.WithInsecure())
	if err != nil {
		t.Fatal(err)
	}
	defer conn.Close()

	expire, _ := ptypes.TimestampProto(time.Now().Add(time.Hour))

	client := tokenstore.NewTokenStoreClient(conn)
	resp, err := client.SetToken(context.TODO(), &tokenstore.SetTokenRequest{
		CharacterID:      1,
		TokenCharacterID: 1,
		AccessToken:      "<KEY>KE",
		RefreshToken:     "<PASSWORD>",
		TokenType:        "Bearer",
		Expiry:           expire,
	})
	if err != nil {
		t.Fatal(err)
{'fraction_non_alphanumeric': 0.164275956284153, 'fraction_numerical': 0.020491803278688523, 'mean_word_length': 4.354661791590494, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 2, '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': '4613465', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1108, 'n_words': 164}