Beispiel #1
0
func (b *Bomberman) manageBombs() {
	timeBombs := map[string]*timebomb.TimeBomb{}

	for {
		select {
		case bombSignal := <-b.bomb:
			switch bombSignal.Action {
			case strap:
				container := bombSignal.StrapContainer

				if b.backend.GraceTime(container) == 0 {
					continue
				}

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

				timeBombs[container.Handle()] = bomb
				bomb.Strap()

			case defuse:
				bomb, found := timeBombs[bombSignal.DefuseHandle]
				if !found {
					continue
				}

				bomb.Defuse()

				delete(timeBombs, bombSignal.DefuseHandle)
			}
		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.cleanup:
			delete(timeBombs, handle)
		}
	}
}
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"code.cloudfoundry.org/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()

			Ω((<-detonated).Sub(before)).Should(BeNumerically(">=", countdown))
		})

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

			countdown := 100 * time.Millisecond