github.com/rosedblabs/rosedb/v2@v2.3.7-0.20240423093736-a89ea823e5b9/watch_test.go (about)

     1  package rosedb
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  
     7  	"github.com/rosedblabs/rosedb/v2/utils"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestWatch_Insert_Scan(t *testing.T) {
    12  	capacity := 1000
    13  	// There are two spaces to determine whether the queue is full and overwrite the write.
    14  	size := capacity - 2
    15  	q := make([][2][]byte, 0, size)
    16  	w := NewWatcher(uint64(capacity))
    17  	for i := 0; i < size; i++ {
    18  		key := utils.GetTestKey(rand.Int())
    19  		value := utils.RandomValue(128)
    20  		q = append(q, [2][]byte{key, value})
    21  		w.putEvent(&Event{
    22  			Action:  WatchActionPut,
    23  			Key:     key,
    24  			Value:   value,
    25  			BatchId: 0,
    26  		})
    27  	}
    28  
    29  	for i := 0; i < size; i++ {
    30  		e := w.getEvent()
    31  		assert.NotEmpty(t, e)
    32  		key := q[i][0]
    33  		assert.Equal(t, key, e.Key)
    34  		value := q[i][1]
    35  		assert.Equal(t, value, e.Value)
    36  	}
    37  }
    38  
    39  func TestWatch_Rotate_Insert_Scan(t *testing.T) {
    40  	capacity := 1000
    41  	q := make([][2][]byte, capacity)
    42  	w := NewWatcher(uint64(capacity))
    43  	for i := 0; i < 2500; i++ {
    44  		key := utils.GetTestKey(rand.Int())
    45  		value := utils.RandomValue(128)
    46  		w.putEvent(&Event{
    47  			Action:  WatchActionPut,
    48  			Key:     key,
    49  			Value:   value,
    50  			BatchId: 0,
    51  		})
    52  		sub := i % capacity
    53  		q[sub] = [2][]byte{key, value}
    54  	}
    55  
    56  	sub := int(w.queue.Front)
    57  	for {
    58  		e := w.getEvent()
    59  		if e == nil {
    60  			break
    61  		}
    62  		key := q[sub][0]
    63  		assert.Equal(t, key, e.Key)
    64  		value := q[sub][1]
    65  		assert.Equal(t, value, e.Value)
    66  		sub = (sub + 1) % capacity
    67  	}
    68  
    69  }
    70  
    71  func TestWatch_Put_Watch(t *testing.T) {
    72  	options := DefaultOptions
    73  	options.WatchQueueSize = 10
    74  	db, err := Open(options)
    75  	assert.Nil(t, err)
    76  	defer destroyDB(db)
    77  
    78  	w, err := db.Watch()
    79  	assert.Nil(t, err)
    80  	for i := 0; i < 50; i++ {
    81  		key := utils.GetTestKey(rand.Int())
    82  		value := utils.RandomValue(128)
    83  		err = db.Put(key, value)
    84  		assert.Nil(t, err)
    85  		event := <-w
    86  		assert.Equal(t, WatchActionPut, event.Action)
    87  		assert.Equal(t, key, event.Key)
    88  		assert.Equal(t, value, event.Value)
    89  	}
    90  }
    91  
    92  func TestWatch_Put_Delete_Watch(t *testing.T) {
    93  	options := DefaultOptions
    94  	options.WatchQueueSize = 10
    95  	db, err := Open(options)
    96  	assert.Nil(t, err)
    97  	defer destroyDB(db)
    98  
    99  	w, err := db.Watch()
   100  	assert.Nil(t, err)
   101  
   102  	key := utils.GetTestKey(rand.Int())
   103  	value := utils.RandomValue(128)
   104  	err = db.Put(key, value)
   105  	assert.Nil(t, err)
   106  	err = db.Delete(key)
   107  	assert.Nil(t, err)
   108  
   109  	for i := 0; i < 2; i++ {
   110  		event := <-w
   111  		assert.Equal(t, key, event.Key)
   112  		if event.Action == WatchActionPut {
   113  			assert.Equal(t, value, event.Value)
   114  		} else if event.Action == WatchActionDelete {
   115  			assert.Equal(t, 0, len(event.Value))
   116  		}
   117  	}
   118  }
   119  
   120  func TestWatch_Batch_Put_Watch(t *testing.T) {
   121  	options := DefaultOptions
   122  	options.WatchQueueSize = 1000
   123  	db, err := Open(options)
   124  	assert.Nil(t, err)
   125  	defer destroyDB(db)
   126  
   127  	w, err := db.Watch()
   128  	assert.Nil(t, err)
   129  
   130  	times := 100
   131  	batch := db.NewBatch(DefaultBatchOptions)
   132  	for i := 0; i < times; i++ {
   133  		err = batch.Put(utils.GetTestKey(rand.Int()), utils.RandomValue(128))
   134  		assert.Nil(t, err)
   135  	}
   136  	err = batch.Commit()
   137  	assert.Nil(t, err)
   138  
   139  	var batchId uint64
   140  	for i := 0; i < times; i++ {
   141  		event := <-w
   142  		if i == 0 {
   143  			batchId = event.BatchId
   144  		}
   145  		assert.Equal(t, batchId, event.BatchId)
   146  	}
   147  }