github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/query/metadata/metaupdater_test.go (about)

     1  /*
     2  Copyright 2023.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package metadata
    18  
    19  import (
    20  	"os"
    21  	"sort"
    22  	"testing"
    23  
    24  	log "github.com/sirupsen/logrus"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func Test_initMockMetadata(t *testing.T) {
    29  
    30  	fileCount := 3
    31  	InitMockColumnarMetadataStore("data/", fileCount, 10, 10)
    32  	assert.Len(t, globalMetadata.allSegmentMicroIndex, fileCount)
    33  	assert.Len(t, globalMetadata.segmentMetadataReverseIndex, fileCount)
    34  	assert.Contains(t, globalMetadata.tableSortedMetadata, "evts")
    35  	assert.Len(t, globalMetadata.tableSortedMetadata["evts"], fileCount)
    36  
    37  	sortedByLatest := sort.SliceIsSorted(globalMetadata.allSegmentMicroIndex, func(i, j int) bool {
    38  		return globalMetadata.allSegmentMicroIndex[i].LatestEpochMS > globalMetadata.allSegmentMicroIndex[j].LatestEpochMS
    39  	})
    40  	assert.True(t, sortedByLatest, "slice is sorted with most recent at the front")
    41  
    42  	tableSorted := globalMetadata.tableSortedMetadata["evts"]
    43  	isTableSorted := sort.SliceIsSorted(tableSorted, func(i, j int) bool {
    44  		return tableSorted[i].LatestEpochMS > tableSorted[j].LatestEpochMS
    45  	})
    46  	assert.True(t, isTableSorted, "slice is sorted with most recent at the front")
    47  
    48  	for i, rawCMI := range tableSorted {
    49  		assert.Equal(t, rawCMI, globalMetadata.allSegmentMicroIndex[i], "bc only one table exists, these sorted slices should point to the same structs")
    50  	}
    51  	for _, rawCMI := range globalMetadata.allSegmentMicroIndex {
    52  		assert.Contains(t, globalMetadata.segmentMetadataReverseIndex, rawCMI.SegmentKey, "all segkeys in allSegmentMicroIndex exist in revserse index")
    53  	}
    54  
    55  	duplicate := globalMetadata.tableSortedMetadata["evts"][0]
    56  	BulkAddSegmentMicroIndex([]*SegmentMicroIndex{duplicate})
    57  	assert.Len(t, globalMetadata.allSegmentMicroIndex, fileCount)
    58  	assert.Len(t, globalMetadata.segmentMetadataReverseIndex, fileCount)
    59  	assert.Contains(t, globalMetadata.tableSortedMetadata, "evts")
    60  	assert.Len(t, globalMetadata.tableSortedMetadata["evts"], fileCount)
    61  
    62  	err := os.RemoveAll("data/")
    63  	if err != nil {
    64  		log.Fatal(err)
    65  	}
    66  }