<commit_before>/**
 * @file scoreboard_test.go
 * @author <NAME> jollheef<AT>riseup.net
 * @license GNU GPLv3
 * @date September, 2015
 * @brief test work with scoreboard
 */

package scoreboard_test

import (
	"log"
	"sort"
	"testing"
)

import "tinfoilhat/scoreboard"

func TestCountScoreboard(*testing.T) {

	res := scoreboard.Result{}

	res.Teams = append(res.Teams, scoreboard.TeamResult{
		Attack:   10,
		Defence:  10,
		Advisory: 50})

	res.Teams = append(res.Teams, scoreboard.TeamResult{
		Attack:   0,
		Defence:  0,
		Advisory: 0})

	res.Teams = append(res.Teams, scoreboard.TeamResult{
		Attack:   100,
		Defence:  100,
		Advisory: 100})

	res.Teams = append(res.Teams, scoreboard.TeamResult{
		Attack:   0,
		Defence:  10,
		Advisory: 10})

	scoreboard.CountScore(&res)

	sort.Sort(scoreboard.ByScore(res.Teams))

	for rank := 1; rank <= 4; rank++ {
		if res.Teams[rank-1].Rank != rank {
			log.Fatalln("team", rank, "rank is not", rank)
		}
	}

	if res.Teams[0].ScorePercent != 100 {
		log.Fatalln("First team score != 100%")
	}
}<commit_msg>Add websocket parallel connect test<commit_after>/**
 * @file scoreboard_test.go
 * @author <NAME> jollheef<AT>riseup.net
 * @license GNU GPLv3
 * @date September, 2015
 * @brief test work with scoreboard
 */

package scoreboard_test

import (
	"database/sql"
	"golang.org/x/net/websocket"
	"log"
	"sort"
	"sync"
	"testing"
	"time"
)

import (
	"tinfoilhat/scoreboard"
	"tinfoilhat/steward"
)

const (
	db_path  string = "user=postgres dbname=tinfoilhat_test sslmode=disable"
	www_path string = "www"
)

func TestCountScoreboard(*testing.T) {

	res := scoreboard.Result{}

	res.Teams = append(res.Teams, scoreboard.TeamResult{
		Attack:   10,
		Defence:  10,
		Advisory: 50})

	res.Teams = append(res.Teams, scoreboard.TeamResult{
		Attack:   0,
		Defence:  0,
		Advisory: 0})

	res.Teams = append(res.Teams, scoreboard.TeamResult{
		Attack:   100,
		Defence:  100,
		Advisory: 100})

	res.Teams = append(res.Teams, scoreboard.TeamResult{
		Attack:   0,
		Defence:  10,
		Advisory: 10})

	scoreboard.CountScore(&res)

	sort.Sort(scoreboard.ByScore(res.Teams))

	for rank := 1; rank <= 4; rank++ {
		if res.Teams[rank-1].Rank != rank {
			log.Fatalln("team", rank, "rank is not", rank)
		}
	}

	if res.Teams[0].ScorePercent != 100 {
		log.Fatalln("First team score != 100%")
	}
}

func dialWebsocket(db *sql.DB, wg *sync.WaitGroup, i int) {

	origin := "http://localhost/"
	url := "ws://localhost:8080/scoreboard"

	ws, err := websocket.Dial(url, "", origin)
	if err != nil {
		log.Fatal(err)
	}

	res, err := scoreboard.CollectLastResult(db)
	if err != nil {
		log.Fatal(err)
	}

	html_res := res.ToHTML()

	var msg = make([]byte, len(html_res))
	if _, err = ws.Read(msg); err != nil {
		log.Fatal(err)
	}

	if string(msg) != html_res {
		log.Fatalln("Received result invalid",
			html_res, msg)
	}

	wg.Done()
}

func TestParallelWebSocketConnect(*testing.T) {

	db, err := steward.OpenDatabase(db_path)
	if err != nil {
		log.Fatal(err)
	}

	db.SetMaxOpenConns(50) // default == 100

	err = steward.CleanDatabase(db)
	if err != nil {
		log.Fatal(err)
	}

	addr := ":8080"

	go func() {
		err := scoreboard.Scoreboard(db, www_path, addr, time.Second,
			time.Now(), time.Minute, time.Minute)
		if err != nil {
			log.Fatal(err)
		}
	}()

	time.Sleep(time.Second) // wait scoreboard launching

	connects := 1000

	log.Printf("%d parallel connects\n", connects)

	var wg sync.WaitGroup
	for i := 0; i < connects; i++ {
		wg.Add(1)
		go dialWebsocket(db, &wg, i)
	}

	wg.Wait()
}
{'fraction_non_alphanumeric': 0.1570458404074703, 'fraction_numerical': 0.026881720430107527, 'mean_word_length': 3.645203679369251, 'pattern_counts': {'":': 1, '<': 10, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, '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': '2264100', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1396, 'n_words': 302}