Skip to content

Commit

Permalink
🍱 [patch] Move finished reverify queues to the last (#6)
Browse files Browse the repository at this point in the history
* 🍱 Move finished reverify queue to the last

* ✅ Update testcase

* 🔧 Not required ci to pass
  • Loading branch information
Pohfy123 authored Mar 23, 2020
1 parent 6b61317 commit 85c19fa
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 4 deletions.
19 changes: 19 additions & 0 deletions api/v1beta1/queue_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ type ByNoOfOrder []Queue

func (q ByNoOfOrder) Len() int { return len(q) }
func (q ByNoOfOrder) Less(i, j int) bool {
now := metav1.Now()

if q[i].Spec.NoOfOrder == q[j].Spec.NoOfOrder {
if q[i].Spec.NextProcessAt == nil {
return true
Expand All @@ -395,6 +397,23 @@ func (q ByNoOfOrder) Less(i, j int) bool {
}
return q[i].Spec.NextProcessAt.Time.Before(q[j].Spec.NextProcessAt.Time)
}

// if next process at is after now, means that the reverify process has been finished
// moves to the last of queue
if q[i].Spec.NextProcessAt != nil && q[i].Spec.NextProcessAt.After(now.Time) &&
q[j].Spec.NextProcessAt != nil && q[j].Spec.NextProcessAt.After(now.Time) {
return q[i].Spec.NextProcessAt.Time.Before(q[j].Spec.NextProcessAt.Time)
}

if q[i].Spec.NextProcessAt != nil && q[i].Spec.NextProcessAt.After(now.Time) {
return false
}

if q[j].Spec.NextProcessAt != nil && q[j].Spec.NextProcessAt.After(now.Time) {
return true
}

// sort by order
return q[i].Spec.NoOfOrder < q[j].Spec.NoOfOrder
}

Expand Down
109 changes: 109 additions & 0 deletions api/v1beta1/queue_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package v1beta1_test

import (
"os"
"testing"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

s2hv1beta1 "github.com/agoda-com/samsahai/api/v1beta1"
s2hlog "github.com/agoda-com/samsahai/internal/log"
"github.com/agoda-com/samsahai/internal/util/unittest"
)

func TestQueueList_Sort(t *testing.T) {
unittest.InitGinkgo(t, "Queue List Sort")
}

var _ = BeforeSuite(func() {
if os.Getenv("DEBUG") != "" {
s2hlog.SetLogger(zap.New(func(o *zap.Options) {
o.Development = true
}))
}
})

var _ = Describe("Sort by no of order", func() {
g := NewWithT(GinkgoT())

beforeNow := metav1.Time{Time: metav1.Now().Add(-10 * time.Minute)}
afterNow := metav1.Time{Time: metav1.Now().Add(10 * time.Minute)}

It("should sort queue list by no of order correctly", func() {
queueList := s2hv1beta1.QueueList{
Items: []s2hv1beta1.Queue{
{Spec: s2hv1beta1.QueueSpec{Name: "comp1", NoOfOrder: 3}},
{Spec: s2hv1beta1.QueueSpec{Name: "comp2", NoOfOrder: 1}},
{Spec: s2hv1beta1.QueueSpec{Name: "comp3", NoOfOrder: 2}},
},
}

expectedQueueList := s2hv1beta1.QueueList{
Items: []s2hv1beta1.Queue{
{Spec: s2hv1beta1.QueueSpec{Name: "comp2", NoOfOrder: 1}},
{Spec: s2hv1beta1.QueueSpec{Name: "comp3", NoOfOrder: 2}},
{Spec: s2hv1beta1.QueueSpec{Name: "comp1", NoOfOrder: 3}},
},
}

queueList.Sort()
g.Expect(queueList.Items).To(BeEquivalentTo(expectedQueueList.Items))
})

It("should sort queue list correctly in case of orders are the same", func() {
queueList := s2hv1beta1.QueueList{
Items: []s2hv1beta1.Queue{
{Spec: s2hv1beta1.QueueSpec{Name: "comp1", NoOfOrder: 1}},
{Spec: s2hv1beta1.QueueSpec{Name: "comp2-2", NoOfOrder: 2}},
{Spec: s2hv1beta1.QueueSpec{Name: "comp2-4", NoOfOrder: 2, NextProcessAt: &beforeNow}},
{Spec: s2hv1beta1.QueueSpec{Name: "comp2-3", NoOfOrder: 2,
NextProcessAt: &metav1.Time{Time: beforeNow.Add(-10 * time.Minute)}}},
{Spec: s2hv1beta1.QueueSpec{Name: "comp2-1", NoOfOrder: 2}},
},
}

expectedQueueList := s2hv1beta1.QueueList{
Items: []s2hv1beta1.Queue{
{Spec: s2hv1beta1.QueueSpec{Name: "comp1", NoOfOrder: 1}},
{Spec: s2hv1beta1.QueueSpec{Name: "comp2-1", NoOfOrder: 2}},
{Spec: s2hv1beta1.QueueSpec{Name: "comp2-2", NoOfOrder: 2}},
{Spec: s2hv1beta1.QueueSpec{Name: "comp2-3", NoOfOrder: 2,
NextProcessAt: &metav1.Time{Time: beforeNow.Add(-10 * time.Minute)}}},
{Spec: s2hv1beta1.QueueSpec{Name: "comp2-4", NoOfOrder: 2, NextProcessAt: &beforeNow}},
},
}

queueList.Sort()
g.Expect(queueList.Items).To(BeEquivalentTo(expectedQueueList.Items))
})

It("should sort queue list correctly in case of finishing reverify process, "+
"next process at is after now", func() {
queueList := s2hv1beta1.QueueList{
Items: []s2hv1beta1.Queue{
{Spec: s2hv1beta1.QueueSpec{Name: "comp1", NoOfOrder: 2}},
{Spec: s2hv1beta1.QueueSpec{Name: "comp2", NoOfOrder: 1,
NextProcessAt: &metav1.Time{Time: afterNow.Add(10 * time.Minute)}}}, // finished reverify process
{Spec: s2hv1beta1.QueueSpec{Name: "comp3", NoOfOrder: 4, NextProcessAt: &afterNow}}, // finished reverify process
{Spec: s2hv1beta1.QueueSpec{Name: "comp4", NoOfOrder: 3, NextProcessAt: &beforeNow}},
},
}

expectedQueueList := s2hv1beta1.QueueList{
Items: []s2hv1beta1.Queue{
{Spec: s2hv1beta1.QueueSpec{Name: "comp1", NoOfOrder: 2}},
{Spec: s2hv1beta1.QueueSpec{Name: "comp4", NoOfOrder: 3, NextProcessAt: &beforeNow}},
{Spec: s2hv1beta1.QueueSpec{Name: "comp3", NoOfOrder: 4, NextProcessAt: &afterNow}}, // finished reverify process
{Spec: s2hv1beta1.QueueSpec{Name: "comp2", NoOfOrder: 1,
NextProcessAt: &metav1.Time{Time: afterNow.Add(10 * time.Minute)}}}, // finished reverify process
},
}

queueList.Sort()
g.Expect(queueList.Items).To(BeEquivalentTo(expectedQueueList.Items))
})
})
4 changes: 0 additions & 4 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
codecov:
notify:
require_ci_to_pass: yes

coverage:
precision: 2
round: down
Expand Down

0 comments on commit 85c19fa

Please sign in to comment.