github.com/songzhibin97/gkit@v1.2.13/page_token/token_test.go (about)

     1  package page_token
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestNewToken(t *testing.T) {
    11  	n := NewTokenGenerate("test")
    12  
    13  	i, err := n.GetIndex(n.ForIndex(1))
    14  	assert.NoError(t, err)
    15  	assert.Equal(t, i, 1)
    16  
    17  	n2 := NewTokenGenerate("test", SetSalt("test1"))
    18  	s2 := n2.ForIndex(1)
    19  	_, err = n.GetIndex(s2)
    20  	assert.Error(t, err)
    21  	assert.Equal(t, err, ErrInvalidToken)
    22  
    23  	n3 := NewTokenGenerate("test", SetMaxIndex(10))
    24  
    25  	_, err = n3.GetIndex(n3.ForIndex(11))
    26  	assert.Error(t, err)
    27  	assert.Equal(t, err, ErrOverMaxPageSizeToken)
    28  
    29  	n4 := NewTokenGenerate("test", SetTimeLimitation(time.Second*5))
    30  	s4 := n4.ForIndex(1)
    31  	time.Sleep(6 * time.Second)
    32  	_, err = n4.GetIndex(s4)
    33  	assert.Error(t, err)
    34  	assert.Equal(t, err, ErrOverdueToken)
    35  }
    36  
    37  func Test_token_ProcessPageTokens(t *testing.T) {
    38  	n := NewTokenGenerate("test")
    39  	s, e, tk, err := n.ProcessPageTokens(10, 1, "")
    40  	t.Log(s, e, tk)
    41  	assert.NoError(t, err)
    42  	for tk != "" {
    43  		s, e, tk, err = n.ProcessPageTokens(10, 3, tk)
    44  		t.Log(s, e, tk)
    45  		assert.NoError(t, err)
    46  	}
    47  }