<commit_before><commit_msg>unit tests for nonce table<commit_after>package zwave

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestGetSetNonce(t *testing.T) {
	table := NewNonceTable()

	table.Set(0x00, []byte{0x00, 0x01, 0x02, 0x03}, time.Second)

	nonce, err := table.Get(0x00)
	assert.NoError(t, err)
	assert.EqualValues(t, []byte{0x00, 0x01, 0x02, 0x03}, nonce)

	nonce, err = table.Get(0x01)
	assert.Error(t, err)
	assert.Nil(t, nonce)
}

func TestGetDeletesItem(t *testing.T) {
	table := NewNonceTable()

	table.Set(0x00, []byte{0x00, 0x01, 0x02, 0x03}, time.Second)

	nonce, err := table.Get(0x00)
	assert.NoError(t, err)
	assert.EqualValues(t, []byte{0x00, 0x01, 0x02, 0x03}, nonce)

	nonce, err = table.Get(0x00)
	assert.Error(t, err)
	assert.Nil(t, nonce)
}

func TestDeleteItem(t *testing.T) {
	table := NewNonceTable()

	table.Set(0x00, []byte{0x00, 0x01, 0x02, 0x03}, time.Second)
	table.Delete(0x00)
	nonce, err := table.Get(0x00)

	assert.Error(t, err)
	assert.Nil(t, nonce)
}

func TestGenerate(t *testing.T) {
	table := NewNonceTable()

	nonce, err := table.Generate(time.Second)
	assert.NoError(t, err)
	assert.Len(t, nonce, 8)
}

func TestNoncesTimeOut(t *testing.T) {
	table := NewNonceTable()

	table.Set(0x00, []byte{0x00}, time.Microsecond*10)
	time.Sleep(time.Microsecond * 50)

	nonce, err := table.Get(0x00)
	assert.Error(t, err)
	assert.Nil(t, nonce)
}

func TestNonceSetResetsTimeout(t *testing.T) {
	table := NewNonceTable()

	table.Set(0x00, []byte{0x00}, time.Millisecond*100)
	time.Sleep(time.Millisecond * 50)

	table.Set(0x00, []byte{0x01}, time.Millisecond*250)
	time.Sleep(time.Millisecond * 100)

	nonce, err := table.Get(0x00)
	assert.NoError(t, err)
	assert.EqualValues(t, []byte{0x01}, nonce)

	table.Set(0x00, []byte{0x00}, time.Millisecond*100)
	time.Sleep(time.Millisecond * 50)

	table.Set(0x00, []byte{0x01}, time.Millisecond*250)
	time.Sleep(time.Millisecond * 300)

	nonce, err = table.Get(0x00)
	assert.Error(t, err)
	assert.Nil(t, nonce)
}
{'fraction_non_alphanumeric': 0.17145688800792863, 'fraction_numerical': 0.07730426164519326, 'mean_word_length': 5.099697885196375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4114361', 'n_tokens_mistral': 992, 'n_tokens_neox': 886, 'n_words': 157}