func (s *BinPackScheduler) MatchOffers(offers []*mesos.Offer, lrpAuctions []auctiontypes.LRPAuction, taskAuctions []auctiontypes.TaskAuction) map[string] /*slaveId*/ *OfferMatch {
	sort.Sort(auctionrunner.SortableLRPAuctions(lrpAuctions))
	sort.Sort(auctionrunner.SortableTaskAuctions(taskAuctions))
	offerAggregates := aggregateOffersBySlave(offers)
	sort.Sort(binpackSortableOfferAggregates{offerAggregates: offerAggregates, registry: s.registry})

	matches := make(map[string]*OfferMatch)
	for _, offers := range offerAggregates {
		matches[offers[0].SlaveId.GetValue()] = &OfferMatch{Offers: offers}
	}
	matches[""] = &OfferMatch{}

	for _, lrpAuction := range lrpAuctions {
		_, offers := matchLrpAuction(offerAggregates, &lrpAuction)
		if offers != nil {
			matches[offers[0].SlaveId.GetValue()].LrpAuctions = append(matches[offers[0].SlaveId.GetValue()].LrpAuctions, lrpAuction)
		} else {
			matches[""].LrpAuctions = append(matches[""].LrpAuctions, lrpAuction)
		}
	}
	for _, taskAuction := range taskAuctions {
		_, offers := matchTaskAuction(offerAggregates, &taskAuction)
		if offers != nil {
			matches[offers[0].SlaveId.GetValue()].TaskAuctions = append(matches[offers[0].SlaveId.GetValue()].TaskAuctions, taskAuction)
		} else {
			matches[""].TaskAuctions = append(matches[""].TaskAuctions, taskAuction)
		}
	}

	return matches
}
func (s *SpreadScheduler) MatchOffers(offers []*mesos.Offer, lrpAuctions []auctiontypes.LRPAuction, taskAuctions []auctiontypes.TaskAuction) map[string]*OfferMatch {
	s.pendingRegistry = NewTaskRegistry()

	sort.Sort(auctionrunner.SortableLRPAuctions(lrpAuctions))
	sort.Sort(auctionrunner.SortableTaskAuctions(taskAuctions))
	offerAggregates := aggregateOffersBySlave(offers)

	matches := make(map[string]*OfferMatch)
	for _, offers := range offerAggregates {
		matches[offers[0].SlaveId.GetValue()] = &OfferMatch{Offers: offers}
	}
	matches[""] = &OfferMatch{}

	for _, lrpAuction := range lrpAuctions {
		sort.Sort(spreadSortableOfferAggregatesForLrp{offerAggregates: offerAggregates, registry: s.registry, pendingRegistry: s.pendingRegistry, lrpGuid: lrpAuction.ProcessGuid})
		_, offers := matchLrpAuction(offerAggregates, &lrpAuction)
		if offers != nil {
			matches[offers[0].SlaveId.GetValue()].LrpAuctions = append(matches[offers[0].SlaveId.GetValue()].LrpAuctions, lrpAuction)
			s.pendingRegistry.AddLrp(offers[0].SlaveId.GetValue(), lrpAuction.ProcessGuid, lrpAuction.Index, mesos.TaskState_TASK_STAGING)
		} else {
			matches[""].LrpAuctions = append(matches[""].LrpAuctions, lrpAuction)
		}
	}
	for _, taskAuction := range taskAuctions {
		sort.Sort(spreadSortableOfferAggregatesForTask{offerAggregates: offerAggregates, registry: s.registry, pendingRegistry: s.pendingRegistry})
		_, offers := matchTaskAuction(offerAggregates, &taskAuction)
		if offers != nil {
			matches[offers[0].SlaveId.GetValue()].TaskAuctions = append(matches[offers[0].SlaveId.GetValue()].TaskAuctions, taskAuction)
			s.pendingRegistry.AddTask(offers[0].SlaveId.GetValue(), taskAuction.TaskGuid, mesos.TaskState_TASK_STAGING)
		} else {
			matches[""].TaskAuctions = append(matches[""].TaskAuctions, taskAuction)
		}
	}

	s.pendingRegistry = nil
	return matches
}
import (
	"sort"
	"time"

	"github.com/cloudfoundry-incubator/auction/auctionrunner"
	"github.com/cloudfoundry-incubator/auction/auctiontypes"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Sortable Auctions", func() {
	Describe("LRP Auctions", func() {
		var lrps []auctiontypes.LRPAuction

		JustBeforeEach(func() {
			sort.Sort(auctionrunner.SortableLRPAuctions(lrps))
		})

		Context("when LRP indexes match", func() {
			BeforeEach(func() {
				lrps = []auctiontypes.LRPAuction{
					BuildLRPAuction("pg-6", "domain", 0, "linux", 10, 10, time.Time{}),
					BuildLRPAuction("pg-7", "domain", 0, "linux", 20, 10, time.Time{}),
					BuildLRPAuction("pg-8", "domain", 0, "linux", 30, 10, time.Time{}),
					BuildLRPAuction("pg-9", "domain", 0, "linux", 40, 10, time.Time{}),
				}
			})

			It("sorts boulders before pebbles", func() {
				Expect(lrps[0].ProcessGuid).To((Equal("pg-9")))
				Expect(lrps[1].ProcessGuid).To((Equal("pg-8")))