<commit_before>// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package addressupdater_test

import (
	"fmt"
	"reflect"
	"time"

	gc "launchpad.net/gocheck"

	"launchpad.net/juju-core/instance"
	"launchpad.net/juju-core/juju/testing"
	"launchpad.net/juju-core/provider/dummy"
	"launchpad.net/juju-core/state"
	coretesting "launchpad.net/juju-core/testing"
	"launchpad.net/juju-core/testing/testbase"
	"launchpad.net/juju-core/worker"
	"launchpad.net/juju-core/worker/addressupdater"
)

var _ = gc.Suite(&workerSuite{})

type workerSuite struct {
	testing.JujuConnSuite
}

func (*workerSuite) instId(i int) instance.Id {
	return instance.Id(fmt.Sprint(i))
}

func (*workerSuite) addressesForIndex(i int) []instance.Address {
	return []instance.Address{
		instance.NewAddress(fmt.Sprintf("127.0.0.%d", i)),
	}
}

func (s *workerSuite) TestWorker(c *gc.C) {
	// Most functionality is already tested in detail - we
	// just need to test that things are wired together
	// correctly.
	defer testbase.PatchValue(addressupdater.ShortPoll, 10*time.Millisecond).Restore()
	defer testbase.PatchValue(addressupdater.LongPoll, 10*time.Millisecond).Restore()
	machines, insts := s.setupScenario(c)
	s.State.StartSync()
	w := addressupdater.NewWorker(s.State)
	defer func() {
		c.Assert(worker.Stop(w), gc.IsNil)
	}()

	// Wait for the odd numbered machines in the
	// first half of the machine slice to be given their
	// addresses.
	for a := coretesting.LongAttempt.Start(); a.Next(); {
		if !a.HasNext() {
			c.Fatalf("timed out waiting for machine addresses")
		}
		if machinesSatisfy(c, machines, func(i int, m *state.Machine) bool {
			if i < len(machines)/2 && i%2 == 1 {
				return reflect.DeepEqual(m.Addresses(), s.addressesForIndex(i))
			}
			return len(m.Addresses()) == 0
		}) {
			break
		}
	}
	// Now provision the second half of the machines and
	// watch them get addresses.
	for i := len(insts) / 2; i < len(insts); i++ {
		dummy.SetInstanceAddresses(insts[i], s.addressesForIndex(i))
	}
	for a := coretesting.LongAttempt.Start(); a.Next(); {
		if !a.HasNext() {
			c.Fatalf("timed out waiting for machine addresses")
		}
		if machinesSatisfy(c, machines, func(i int, m *state.Machine) bool {
			return reflect.DeepEqual(m.Addresses(), s.addressesForIndex(i))
		}) {
			break
		}
	}
}

func machinesSatisfy(c *gc.C, machines []*state.Machine, f func(i int, m *state.Machine) bool) bool {
	for i, m := range machines {
		err := m.Refresh()
		c.Assert(err, gc.IsNil)
		if !f(i, m) {
			return false
		}
	}
	return true
}

func (s *workerSuite) setupScenario(c *gc.C) ([]*state.Machine, []instance.Instance) {
	var machines []*state.Machine
	var insts []instance.Instance
	for i := 0; i < 10; i++ {
		m, err := s.State.AddMachine("series", state.JobHostUnits)<commit_msg>add TODO<commit_after>// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package addressupdater_test

import (
	"fmt"
	"reflect"
	"time"

	gc "launchpad.net/gocheck"

	"launchpad.net/juju-core/instance"
	"launchpad.net/juju-core/juju/testing"
	"launchpad.net/juju-core/provider/dummy"
	"launchpad.net/juju-core/state"
	coretesting "launchpad.net/juju-core/testing"
	"launchpad.net/juju-core/testing/testbase"
	"launchpad.net/juju-core/worker"
	"launchpad.net/juju-core/worker/addressupdater"
)

var _ = gc.Suite(&workerSuite{})

type workerSuite struct {
	testing.JujuConnSuite
}

func (*workerSuite) instId(i int) instance.Id {
	return instance.Id(fmt.Sprint(i))
}

func (*workerSuite) addressesForIndex(i int) []instance.Address {
	return []instance.Address{
		instance.NewAddress(fmt.Sprintf("127.0.0.%d", i)),
	}
}

func (s *workerSuite) TestWorker(c *gc.C) {
	// Most functionality is already tested in detail - we
	// just need to test that things are wired together
	// correctly.
	defer testbase.PatchValue(addressupdater.ShortPoll, 10*time.Millisecond).Restore()
	defer testbase.PatchValue(addressupdater.LongPoll, 10*time.Millisecond).Restore()
	machines, insts := s.setupScenario(c)
	s.State.StartSync()
	w := addressupdater.NewWorker(s.State)
	defer func() {
		c.Assert(worker.Stop(w), gc.IsNil)
	}()

	// Wait for the odd numbered machines in the
	// first half of the machine slice to be given their
	// addresses.
	for a := coretesting.LongAttempt.Start(); a.Next(); {
		if !a.HasNext() {
			c.Fatalf("timed out waiting for machine addresses")
		}
		if machinesSatisfy(c, machines, func(i int, m *state.Machine) bool {
			if i < len(machines)/2 && i%2 == 1 {
				return reflect.DeepEqual(m.Addresses(), s.addressesForIndex(i))
			}
			return len(m.Addresses()) == 0
		}) {
			break
		}
	}
	// Now provision the second half of the machines and
	// watch them get addresses.
	for i := len(insts) / 2; i < len(insts); i++ {
		dummy.SetInstanceAddresses(insts[i], s.addressesForIndex(i))
	}
	for a := coretesting.LongAttempt.Start(); a.Next(); {
		if !a.HasNext() {
			c.Fatalf("timed out waiting for machine addresses")
		}
		if machinesSatisfy(c, machines, func(i int, m *state.Machine) bool {
			return reflect.DeepEqual(m.Addresses(), s.addressesForIndex(i))
		}) {
			break
		}
	}
}

// TODO(rog)
// - check that the environment observer is actually hooked up.
// - check that the environment observer is stopped.
// - check that the errors propagate correctly.

func machinesSatisfy(c *gc.C, machines []*state.Machine, f func(i int, m *state.Machine) bool) bool {
	for i, m := range machines {
		err := m.Refresh()
		c.Assert(err, gc.IsNil)
		if !f(i, m) {
			return false
		}
	}
	return true
}

func (s *workerSuite) setupScenario(c *gc.C) ([]*state.Machine, []instance.Instance) {
	var machines []*state.Machine
	var insts []instance.Instance
	for i := 0; i < 10; i++ {
		m, err := s.State.AddMachine("series", state.JobHostUnits)
{'fraction_non_alphanumeric': 0.14836488812392426, 'fraction_numerical': 0.007573149741824441, 'mean_word_length': 3.979434447300771, 'pattern_counts': {'":': 0, '<': 9, '<?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': '2475600', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2075, 'n_words': 503}