github.com/tsuna/gohbase@v0.0.0-20250731002811-4ffcadfba63e/hrpc/scan_test.go (about)

     1  // Copyright (C) 2025  The GoHBase Authors.  All rights reserved.
     2  // This file is part of GoHBase.
     3  // Use of this source code is governed by the Apache License 2.0
     4  // that can be found in the COPYING file.
     5  
     6  package hrpc
     7  
     8  import (
     9  	"context"
    10  	"testing"
    11  )
    12  
    13  func TestWithScanStatsHandler(t *testing.T) {
    14  	ctx := context.Background()
    15  	table := []byte("random table")
    16  
    17  	s, err := NewScan(ctx, table)
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  
    22  	if s.ScanStatsHandler() != nil {
    23  		t.Fatal("ScanStatsHandler is not nil as expected")
    24  	}
    25  
    26  	h := func(stats *ScanStats) {}
    27  
    28  	s, err = NewScan(ctx, table, WithScanStatsHandler(h))
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  
    33  	if s.ScanStatsHandler() == nil {
    34  		t.Fatal("ScanStatsHandler nil, want it to be set")
    35  	}
    36  
    37  	s, err = NewScan(ctx, table, WithScanStatsHandler(nil))
    38  	if err == nil {
    39  		t.Fatal("nil handler should have returned an error")
    40  	}
    41  
    42  	_, err = NewGet(ctx, table, []byte("random key"), WithScanStatsHandler(h))
    43  	if err == nil {
    44  		t.Fatal("Should not have been able to create non-Scan request without error")
    45  	}
    46  }
    47  
    48  func TestScanStatsID(t *testing.T) {
    49  	var (
    50  		ctx        = context.Background()
    51  		table      = []byte("random table")
    52  		expectedID = int64(11)
    53  	)
    54  
    55  	s, err := NewScan(ctx, table, ScanStatsID(expectedID))
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	if s.ScanStatsID() != expectedID {
    60  		t.Fatalf("ScanStatsID = %d, expected %d", s.ScanStatsID(), expectedID)
    61  	}
    62  
    63  	s, err = NewScan(ctx, table)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	if s.ScanStatsID() == expectedID {
    68  		t.Fatalf("ScanStatsID on a new Scan request should be reset to a random num")
    69  	}
    70  
    71  	_, err = NewGet(ctx, table, []byte("random key"), ScanStatsID(expectedID))
    72  	if err == nil {
    73  		t.Fatal("Should not have been able to create non-Scan request without error")
    74  	}
    75  }