Пример #1
0
func (b *Bomberman) manageBombs() {
	timeBombs := map[string]*timebomb.TimeBomb{}

	for {
		select {
		case container := <-b.strap:
			if container.GraceTime() == 0 {
				continue
			}

			bomb := timebomb.New(
				container.GraceTime(),
				func() {
					b.detonate(container)
					b.defuse <- container.Handle()
				},
			)

			timeBombs[container.Handle()] = bomb

			bomb.Strap()

		case handle := <-b.pause:
			bomb, found := timeBombs[handle]
			if !found {
				continue
			}

			bomb.Pause()

		case handle := <-b.unpause:
			bomb, found := timeBombs[handle]
			if !found {
				continue
			}

			bomb.Unpause()

		case handle := <-b.defuse:
			bomb, found := timeBombs[handle]
			if !found {
				continue
			}

			bomb.Defuse()

			delete(timeBombs, handle)
		}
	}
}
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"github.com/pivotal-cf-experimental/garden/server/timebomb"
)

var _ = Describe("THE TIMEBOMB", func() {
	Context("WHEN STRAPPED", func() {
		It("DETONATES AFTER THE COUNTDOWN", func() {
			detonated := make(chan time.Time)

			countdown := 100 * time.Millisecond

			bomb := timebomb.New(
				countdown,
				func() {
					detonated <- time.Now()
				},
			)

			before := time.Now()

			bomb.Strap()

			Expect((<-detonated).Sub(before)).To(BeNumerically(">=", countdown))
		})

		It("DOES NOT DETONATE AGAIN", func() {
			detonated := make(chan time.Time)

			countdown := 100 * time.Millisecond