github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/graveler/ref/commit_generation_priority_queue_test.go (about)

     1  package ref_test
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/treeverse/lakefs/pkg/graveler"
     9  	"github.com/treeverse/lakefs/pkg/graveler/ref"
    10  )
    11  
    12  func TestCommitsGenerationPriorityQueue_Len(t *testing.T) {
    13  	const maxItems = 7
    14  	q := ref.NewCommitsGenerationPriorityQueue()
    15  	for i := 0; i < maxItems; i++ {
    16  		if q.Len() != i {
    17  			t.Fatalf("Queue Len=%d, expected=%d", q.Len(), i)
    18  		}
    19  		id := graveler.CommitID(strconv.Itoa(i))
    20  		q.Push(&graveler.CommitRecord{CommitID: id})
    21  	}
    22  	if q.Len() != maxItems {
    23  		t.Fatalf("Queue Len=%d, expected=%d", q.Len(), maxItems)
    24  	}
    25  }
    26  
    27  func TestCommitsGenerationPriorityQueue_Swap(t *testing.T) {
    28  	rec1 := &graveler.CommitRecord{CommitID: "1"}
    29  	rec2 := &graveler.CommitRecord{CommitID: "2"}
    30  
    31  	q := ref.NewCommitsGenerationPriorityQueue()
    32  	q.Push(rec1)
    33  	q.Push(rec2)
    34  	q.Swap(0, 1)
    35  	if q[0].CommitID != rec2.CommitID && q[1].CommitID != rec1.CommitID {
    36  		t.Fatal("Swap expected to replace records positions")
    37  	}
    38  }
    39  
    40  func TestCommitsGenerationPriorityQueue_Push(t *testing.T) {
    41  	q := ref.NewCommitsGenerationPriorityQueue()
    42  	rec1 := &graveler.CommitRecord{CommitID: "1"}
    43  	q.Push(rec1)
    44  	if len(q) != 1 || q[0] != rec1 {
    45  		t.Fatalf("Push() failed first record - len=%d", len(q))
    46  	}
    47  	rec2 := &graveler.CommitRecord{CommitID: "2"}
    48  	q.Push(rec2)
    49  	if len(q) != 2 || q[1] != rec2 {
    50  		t.Fatalf("Push() failed second record - len=%d", len(q))
    51  	}
    52  }
    53  
    54  func TestCommitsGenerationPriorityQueue_Pop(t *testing.T) {
    55  	const maxItems = 7
    56  	q := ref.NewCommitsGenerationPriorityQueue()
    57  	for i := 0; i < maxItems; i++ {
    58  		id := graveler.CommitID(strconv.Itoa(i))
    59  		q.Push(&graveler.CommitRecord{CommitID: id})
    60  	}
    61  	for i := 0; i < maxItems; i++ {
    62  		item := q.Pop().(*graveler.CommitRecord)
    63  		expectedID := graveler.CommitID(strconv.Itoa(maxItems - i - 1))
    64  		if item.CommitID != expectedID {
    65  			t.Fatalf("Pop() item ID=%s, expected=%s", item.CommitID, expectedID)
    66  		}
    67  	}
    68  	if l := q.Len(); l != 0 {
    69  		t.Fatalf("Pop() all items - Len=%d, expected=0", l)
    70  	}
    71  }
    72  
    73  func TestCommitsGenerationPriorityQueue_Less(t *testing.T) {
    74  	ts1 := time.Now()
    75  	ts2 := ts1.Add(time.Minute)
    76  	tests := []struct {
    77  		Name     string
    78  		Commit1  graveler.Commit
    79  		Commit2  graveler.Commit
    80  		Expected bool
    81  	}{
    82  		{Name: "generation_ascend", Commit1: graveler.Commit{Generation: 0}, Commit2: graveler.Commit{Generation: 1}, Expected: false},
    83  		{Name: "generation_descent", Commit1: graveler.Commit{Generation: 1}, Commit2: graveler.Commit{Generation: 0}, Expected: true},
    84  		{Name: "generation_ascend_creation_ascend", Commit1: graveler.Commit{Generation: 0, CreationDate: ts1}, Commit2: graveler.Commit{Generation: 1, CreationDate: ts2}, Expected: false},
    85  		{Name: "generation_descent_creation_descent", Commit1: graveler.Commit{Generation: 1, CreationDate: ts2}, Commit2: graveler.Commit{Generation: 0, CreationDate: ts1}, Expected: true},
    86  		{Name: "same_generation_creation_ascend", Commit1: graveler.Commit{Generation: 0, CreationDate: ts1}, Commit2: graveler.Commit{Generation: 0, CreationDate: ts2}, Expected: false},
    87  		{Name: "same_generation_creation_descent", Commit1: graveler.Commit{Generation: 0, CreationDate: ts2}, Commit2: graveler.Commit{Generation: 0, CreationDate: ts1}, Expected: true},
    88  		{Name: "same_generation_and_creation", Commit1: graveler.Commit{Generation: 0, CreationDate: ts1}, Commit2: graveler.Commit{Generation: 0, CreationDate: ts1}, Expected: false},
    89  	}
    90  	for _, tt := range tests {
    91  		tt := tt
    92  		t.Run(tt.Name, func(t *testing.T) {
    93  			// setup
    94  			q := ref.NewCommitsGenerationPriorityQueue()
    95  			q.Push(&graveler.CommitRecord{CommitID: "1", Commit: &tt.Commit1})
    96  			q.Push(&graveler.CommitRecord{CommitID: "2", Commit: &tt.Commit2})
    97  			result := q.Less(0, 1)
    98  			if result != tt.Expected {
    99  				t.Fatalf("Less() result=%t, expected=%t", result, tt.Expected)
   100  			}
   101  		})
   102  	}
   103  }