. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"github.com/samuel/go-zookeeper/zk"
	"io/ioutil"
	"log"
	"os"

	"time"
)

var _ = Describe("ZookeeperStoreAdapter", func() {
	var (
		adapter      StoreAdapter
		client       *zk.Conn
		nodeArr      []StoreNode
		timeProvider *faketimeprovider.FakeTimeProvider
		creationTime time.Time
	)

	BeforeEach(func() {
		var err error
		client, _, err = zk.Connect(zookeeperRunner.NodeURLS(), time.Second)
		Ω(err).ShouldNot(HaveOccured())

		timeProvider = &faketimeprovider.FakeTimeProvider{}

		adapter = NewZookeeperStoreAdapter(zookeeperRunner.NodeURLS(), 100, timeProvider, time.Second)
		err = adapter.Connect()
		Ω(err).ShouldNot(HaveOccured())
	. "github.com/cloudfoundry/hm9000/models"
	. "github.com/cloudfoundry/hm9000/testhelpers/app"

	"github.com/cloudfoundry/go_cfmessagebus/fake_cfmessagebus"
	"github.com/cloudfoundry/hm9000/config"
	"github.com/cloudfoundry/hm9000/testhelpers/fakelogger"
	"github.com/cloudfoundry/hm9000/testhelpers/fakestore"
	"github.com/cloudfoundry/hm9000/testhelpers/faketimeprovider"
)

var _ = Describe("Actual state listener", func() {
	var (
		app          App
		anotherApp   App
		store        *fakestore.FakeStore
		listener     *ActualStateListener
		timeProvider *faketimeprovider.FakeTimeProvider
		messageBus   *fake_cfmessagebus.FakeMessageBus
		logger       *fakelogger.FakeLogger
		conf         config.Config
	)

	BeforeEach(func() {
		var err error
		conf, err = config.DefaultConfig()
		Ω(err).ShouldNot(HaveOccured())

		timeProvider = &faketimeprovider.FakeTimeProvider{
			TimeToProvide: time.Now(),
		}

		app = NewApp()
type brokenReader struct{}

func (b *brokenReader) Read([]byte) (int, error) {
	return 0, errors.New("oh no you didn't!")
}

func (b *brokenReader) Close() error {
	return nil
}

var _ = Describe("DesiredStateFetcher", func() {
	var (
		conf           config.Config
		fakeMessageBus *fake_cfmessagebus.FakeMessageBus
		fetcher        *DesiredStateFetcher
		httpClient     *fakehttpclient.FakeHttpClient
		timeProvider   *faketimeprovider.FakeTimeProvider
		store          *fakestore.FakeStore
		resultChan     chan DesiredStateFetcherResult
	)

	BeforeEach(func() {
		var err error
		conf, err = config.DefaultConfig()
		Ω(err).ShouldNot(HaveOccured())

		resultChan = make(chan DesiredStateFetcherResult, 1)
		timeProvider = &faketimeprovider.FakeTimeProvider{
			TimeToProvide: time.Now(),
		}