Example #1
0
func (a *AllocationStore) Allocate(logger lager.Logger, req *executor.AllocationRequest) (executor.Container, error) {
	a.lock.Lock()
	defer a.lock.Unlock()

	if _, err := a.lookup(req.Guid); err == nil {
		logger.Error("failed-allocating-container", err)
		return executor.Container{}, executor.ErrContainerGuidNotAvailable
	}

	logger.Debug("allocating-container", lager.Data{"allocation-request": req})
	defer logger.Debug("finished-allocating-container", lager.Data{"allocation-request": req})

	a.allocated[req.Guid] = executor.NewReservedContainerFromAllocationRequest(req, a.clock.Now().UnixNano())

	a.eventEmitter.Emit(executor.NewContainerReservedEvent(a.allocated[req.Guid]))

	return a.allocated[req.Guid].Copy(), nil
}
		BeforeEach(func() {
			resource := executor.NewResource(512, 512, "")
			req = executor.NewAllocationRequest("banana", &resource, nil)
		})

		Context("when the guid is available", func() {
			It("it is marked as RESERVED", func() {
				allocation, err := allocationStore.Allocate(logger, &req)
				Expect(err).NotTo(HaveOccurred())

				Expect(allocation.Guid).To(Equal(req.Guid))
				Expect(allocation.State).To(Equal(executor.StateReserved))
				Expect(allocation.AllocatedAt).To(Equal(currentTime.UnixNano()))

				Expect(fakeEventEmitter.EmitCallCount()).To(Equal(1))
				Expect(fakeEventEmitter.EmitArgsForCall(0)).To(Equal(executor.NewContainerReservedEvent(allocation)))
			})
		})

		Context("when the guid is not available", func() {
			BeforeEach(func() {
				_, err := allocationStore.Allocate(logger, &req)
				Expect(err).NotTo(HaveOccurred())
			})

			It("errors and does not store the duplicate", func() {
				_, err := allocationStore.Allocate(logger, &req)
				Expect(err).To(HaveOccurred())
				Expect(allocationStore.List()).To(HaveLen(1))
			})
		})