<commit_before>package kontrol

import (
	"fmt"
	"github.com/coreos/go-etcd/etcd"
	"io/ioutil"
	"koding/db/mongodb/modelhelper"
	"koding/newkite/kite"
	"koding/newkite/protocol"
	"labix.org/v2/mgo/bson"
	"testing"
)

func setupTest(t *testing.T) {
	kodingKey := modelhelper.NewKodingKeys()
	kodingKey.Id = bson.ObjectIdHex("528e1b36a819430000000001")
	kodingKey.Key = "<KEY>"
	kodingKey.Owner = "<KEY>"
	kodingKey.Hostname = "tardis.local-39612b01-b08b-4df7-49f7-641e58541459"

	err := modelhelper.AddKodingKeys(kodingKey)
	if err != nil {
		t.Errorf("Cannot add Koding Key to MongoDB: %s", err.Error())
		return
	}

	err = ioutil.WriteFile("/home/vagrant/.kd/koding.key", []byte(kodingKey.Key), 0644)
	if err != nil {
		t.Errorf("Cannot write Koding Key to disk: %s", err.Error())
		return
	}

	etcdClient := etcd.NewClient(nil)
	_, err = etcdClient.DeleteAll("/kites/devrim")
	if err != nil {
		if err.(etcd.EtcdError).ErrorCode != 100 { // Key Not Found
			t.Errorf("Cannot delete keys from etcd: %s", err)
			return
		}
	}
}

func TestKontrol(t *testing.T) {
	setupTest(t)

	kon := New()
	kon.Start()

	mathKite := mathWorker()
	mathKite.Start()

	exp2Kite := exp2()
	exp2Kite.Start()

	query := protocol.KontrolQuery{
		Username:    "devrim",
		Environment: "development",
		Name:        "mathworker",
	}

	kites, err := exp2Kite.Kontrol.GetKites(query, nil)
	if err != nil {
		fmt.Println(err)
		return
	}

	if len(kites) == 0 {
		t.Errorf("No mathworker available")
		return
	}
<commit_msg>kite: add sleep to kontrol test<commit_after>package kontrol

import (
	"fmt"
	"github.com/coreos/go-etcd/etcd"
	"io/ioutil"
	"koding/db/mongodb/modelhelper"
	"koding/newkite/kite"
	"koding/newkite/protocol"
	"labix.org/v2/mgo/bson"
	"testing"
	"time"
)

func setupTest(t *testing.T) {
	kodingKey := modelhelper.NewKodingKeys()
	kodingKey.Id = bson.ObjectIdHex("528e1b36a819430000000001")
	kodingKey.Key = "<KEY>"
	kodingKey.Owner = "<KEY>"
	kodingKey.Hostname = "tardis.local-39612b01-b08b-4df7-49f7-641e58541459"

	err := modelhelper.AddKodingKeys(kodingKey)
	if err != nil {
		t.Errorf("Cannot add Koding Key to MongoDB: %s", err.Error())
		return
	}

	err = ioutil.WriteFile("/home/vagrant/.kd/koding.key", []byte(kodingKey.Key), 0644)
	if err != nil {
		t.Errorf("Cannot write Koding Key to disk: %s", err.Error())
		return
	}

	etcdClient := etcd.NewClient(nil)
	_, err = etcdClient.DeleteAll("/kites/devrim")
	if err != nil {
		if err.(etcd.EtcdError).ErrorCode != 100 { // Key Not Found
			t.Errorf("Cannot delete keys from etcd: %s", err)
			return
		}
	}
}

func TestKontrol(t *testing.T) {
	setupTest(t)

	kon := New()
	kon.Start()

	mathKite := mathWorker()
	mathKite.Start()

	exp2Kite := exp2()
	exp2Kite.Start()

	// Wait for kites to register themselves on Kontrol.
	time.Sleep(500 * time.Millisecond)

	query := protocol.KontrolQuery{
		Username:    "devrim",
		Environment: "development",
		Name:        "mathworker",
	}

	kites, err := exp2Kite.Kontrol.GetKites(query, nil)
	if err != nil {
		fmt.Println(err)
		return
	}

	if len(kites) == 0 {
		t.Errorf("No mathworker available")
		return
	}

{'fraction_non_alphanumeric': 0.15782706525233045, 'fraction_numerical': 0.038894246223079396, 'mean_word_length': 3.877742946708464, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '6806399', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1272, 'n_words': 240}