<commit_before>// Tideland Go Cells - Behaviors - Unit Tests - Evaluator
//
// Copyright (C) 2010-2017 <NAME> / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.

package behaviors_test

//--------------------
// IMPORTS
//--------------------

import (
	"context"
	"strconv"
	"testing"
	"time"

	"github.com/tideland/golib/audit"

	"github.com/tideland/gocells/behaviors"
	"github.com/tideland/gocells/cells"
)

//--------------------
// TESTS
//--------------------

// TestEvaluatorBehavior tests the evaluator behavior. Scenario
// is to wait until the average evaluation has been larger than
// 6.0 for 3 times.
func TestEvaluatorBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	generator := audit.NewGenerator(audit.FixedRand())
	ctx := context.Background()
	env := cells.NewEnvironment("evaluator-behavior")
	defer env.Stop()

	evaluate := func(event cells.Event) (float64, error) {
		i, err := strconv.Atoi(event.Topic())
		assert.Nil(err)
		return float64(i), nil
	}
	matches := func(accessor cells.EventSinkAccessor) cells.CriterionMatch {
		ok, err := accessor.Match(func(index int, event cells.Event) (bool, error) {
			avg, _ := event.Payload().GetFloat64(behaviors.PayloadEvaluationAverage)
			return avg > 6.0, nil
		})
		assert.Nil(err)
		if !ok {
			return cells.CriterionDropLast
		}
		if accessor.Len() < 3 {
			return cells.CriterionKeep
		}
		return cells.CriterionDone
	}
	topics := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}
	waiter := cells.NewPayloadWaiter()

	env.StartCell("evaluator", behaviors.NewEvaluatorBehavior(evaluate))
	env.StartCell("combo", behaviors.NewComboBehavior(matches))
	env.StartCell("waiter", behaviors.NewWaiterBehavior(waiter))
	env.Subscribe("evaluator", "combo")
	env.Subscribe("combo", "waiter")

	go func() {
		for i := 0; i < 10000; i++ {
			topic := generator.OneStringOf(topics...)
			env.EmitNew(ctx, "evaluator", topic, nil)
		}
	}()

	waitCtx, cancel := context.WithTimeout(ctx, time.Second)
	defer cancel()
	payload, err := waiter.Wait(waitCtx)
	assert.Nil(err)
	accessor, ok := payload.Get(behaviors.PayloadComboEvents, nil).(cells.EventSinkAccessor)
	assert.True(ok)
	assert.Length(accessor, 3)
	accessor.Match(func(index int, event cells.Event) (bool, error) {
		avg := event.Payload().GetFloat64(behaviors.PayloadEvaluationAverage, 0.0)
		assert.True(avg > 6.0)
		assert.Logf("avg: %f", avg)
		return false, nil
	})
}

// EOF<commit_msg>Started with evaluator test<commit_after>// Tideland Go Cells - Behaviors - Unit Tests - Evaluator
//
// Copyright (C) 2010-2017 <NAME> / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.

package behaviors_test

//--------------------
// IMPORTS
//--------------------

import (
	"strconv"
	"testing"

	"github.com/tideland/golib/audit"

	"github.com/tideland/gocells/behaviors"
	"github.com/tideland/gocells/cells"
)

//--------------------
// TESTS
//--------------------

// TestEvaluatorBehavior tests the evaluator behavior. Scenario
// is to wait until the average evaluation has been larger than
// 6.0 for 3 times.
func TestEvaluatorBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	sigc := audit.MakeSigChan()
	generator := audit.NewGenerator(audit.FixedRand())
	env := cells.NewEnvironment("evaluator-behavior")
	defer env.Stop()

	evaluator := func(event cells.Event) (float64, error) {
		i, err := strconv.Atoi(event.Topic())
		assert.Nil(err)
		return float64(i), nil
	}
	stopper := func(cell cells.Cell, event cells.Event) error {
		// TODO 2017-06-03 Mue Find criterion for stopping.
	}
	topics := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}

	env.StartCell("evaluator", behaviors.NewEvaluatorBehavior(evaluator))
	env.StartCell("stopper", behaviors.NewSimpleBehavior(stopper))
	env.Subscribe("evaluator", "stopper")

	go func() {
		for i := 0; i < 10000; i++ {
			topic := generator.OneStringOf(topics...)
			env.EmitNew(ctx, "evaluator", topic, nil)
		}
	}()
}

// EOF
{'fraction_non_alphanumeric': 0.19089574155653452, 'fraction_numerical': 0.020558002936857563, 'mean_word_length': 3.380493033226152, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '635202', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1408, 'n_words': 323}