// Copyright 2019 Lunny Xiao. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package levelqueue

import (
	"sync"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/syndtr/goleveldb/leveldb"
)

func TestQueue(t *testing.T) {
	dbDir := t.TempDir()

	queue, err := Open(dbDir)
	require.NoError(t, err)

	err = queue.RPush([]byte("test"))
	require.NoError(t, err)

	assert.EqualValues(t, 1, queue.Len())

	data, err := queue.LPop()
	require.NoError(t, err)
	assert.Equal(t, "test", string(data))

	// should be empty
	data, err = queue.LPop()
	require.Error(t, err)
	assert.Equal(t, []byte(nil), data)
	assert.Equal(t, ErrNotFound, err)

	assert.EqualValues(t, 0, queue.Len())

	err = queue.LPush([]byte("test2"))
	require.NoError(t, err)

	data, err = queue.LPop()
	require.NoError(t, err)
	assert.Equal(t, "test2", string(data))

	assert.EqualValues(t, 0, queue.Len())

	data, err = queue.LPop()
	require.Error(t, err)
	assert.Equal(t, []byte(nil), data)
	assert.Equal(t, ErrNotFound, err)

	data, err = queue.RPop()
	require.Error(t, err)
	assert.Equal(t, []byte(nil), data)
	assert.Equal(t, ErrNotFound, err)

	err = queue.Close()
	require.NoError(t, err)

	queue, err = Open(dbDir)
	require.NoError(t, err)

	err = queue.RPush([]byte("test3"))
	require.NoError(t, err)
	assert.EqualValues(t, 1, queue.Len())

	data, err = queue.RPop()
	require.NoError(t, err)
	assert.Equal(t, "test3", string(data))
}

func TestGoroutines(t *testing.T) {
	dbDir := t.TempDir()

	queue, err := Open(dbDir)
	require.NoError(t, err)

	for range 10 {
		err := queue.RPush([]byte("test"))
		require.NoError(t, err)
	}

	var w sync.WaitGroup
	for i := range 10 {
		w.Add(1)
		go func(i int) {
			if i%2 == 0 {
				err := queue.RPush([]byte("test"))
				assert.NoError(t, err)
			} else {
				_, err := queue.RPop()
				assert.NoError(t, err)
			}
			w.Done()
		}(i)
	}
	w.Wait()
}

func TestGoroutines2(t *testing.T) {
	dbDir := t.TempDir()

	queue, err := Open(dbDir)
	require.NoError(t, err)

	for range 10 {
		err := queue.RPush([]byte("test"))
		require.NoError(t, err)
	}

	var w sync.WaitGroup
	for i := range 10 {
		w.Add(1)
		go func(i int) {
			if i%2 == 0 {
				err := queue.LPush([]byte("test"))
				assert.NoError(t, err)
			} else {
				_, err := queue.RPop()
				assert.NoError(t, err)
			}
			w.Done()
		}(i)
	}
	w.Wait()
}

func TestGoroutines3(t *testing.T) {
	dbDir := t.TempDir()

	queue, err := Open(dbDir)
	require.NoError(t, err)

	for range 10 {
		err := queue.RPush([]byte("test"))
		require.NoError(t, err)
	}

	var w sync.WaitGroup
	for i := range 10 {
		w.Add(1)
		go func(i int) {
			if i%2 == 0 {
				err := queue.LPush([]byte("test"))
				assert.NoError(t, err)
			} else {
				_, err := queue.LPop()
				assert.NoError(t, err)
			}
			w.Done()
		}(i)
	}
	w.Wait()
}

func TestHandle(t *testing.T) {
	dbDir := t.TempDir()

	queue, err := Open(dbDir)
	require.NoError(t, err)

	for range 10 {
		err := queue.RPush([]byte("test"))
		require.NoError(t, err)
	}

	var w sync.WaitGroup
	for i := range 10 {
		w.Add(1)
		go func(i int) {
			if i%2 == 0 {
				err := queue.RPush([]byte("test"))
				assert.NoError(t, err)
			} else {
				assert.NoError(t, queue.RHandle(func([]byte) error {
					return nil
				}))
			}
			w.Done()
		}(i)
	}
	w.Wait()
}

func TestHandle2(t *testing.T) {
	dbDir := t.TempDir()

	queue, err := Open(dbDir)
	require.NoError(t, err)

	for range 10 {
		err := queue.RPush([]byte("test"))
		require.NoError(t, err)
	}
	assert.EqualValues(t, 10, queue.Len())

	var w sync.WaitGroup
	for i := range 10 {
		w.Add(1)
		go func(i int) {
			if i%2 == 0 {
				err := queue.LPush([]byte("test"))
				assert.NoError(t, err)
			} else {
				assert.NoError(t, queue.LHandle(func([]byte) error {
					return nil
				}))
			}
			w.Done()
		}(i)
	}
	w.Wait()
}

func TestFunnilyCorrupted(t *testing.T) {
	dbPath := t.TempDir()

	t.Run("Prepare", func(t *testing.T) {
		queue, err := Open(dbPath)
		require.NoError(t, err)
		defer queue.Close()

		require.NoError(t, queue.RPush([]byte("1")))
		require.NoError(t, queue.RPush([]byte("2")))
		require.NoError(t, queue.RPush([]byte("3")))

		_, err = queue.LPop()
		require.NoError(t, err)
		_, err = queue.LPop()
		require.NoError(t, err)
	})

	t.Run("Corrupt", func(t *testing.T) {
		db, err := leveldb.OpenFile(dbPath, nil)
		require.NoError(t, err)
		defer db.Close()

		require.NoError(t, db.Put([]byte(lowKeyStr), id2bytes(1), nil))
	})

	t.Run("Normal", func(t *testing.T) {
		queue, err := Open(dbPath)
		require.NoError(t, err)
		defer queue.Close()

		data, err := queue.LPop()
		require.NoError(t, err)
		assert.EqualValues(t, "3", data)
	})

	t.Run("Corrupt no items", func(t *testing.T) {
		db, err := leveldb.OpenFile(dbPath, nil)
		require.NoError(t, err)
		defer db.Close()

		require.NoError(t, db.Put([]byte(lowKeyStr), id2bytes(1), nil))
	})

	t.Run("Normal no items", func(t *testing.T) {
		queue, err := Open(dbPath)
		require.NoError(t, err)
		defer queue.Close()

		data, err := queue.LPop()
		assert.Nil(t, data)
		require.ErrorIs(t, err, ErrNotFound)

		require.NoError(t, queue.RPush([]byte("4")))

		data, err = queue.LPop()
		require.NoError(t, err)
		assert.EqualValues(t, "4", data)
	})
}

func BenchmarkPush(b *testing.B) {
	dbDir := b.TempDir()

	queue, err := Open(dbDir)
	require.NoError(b, err)

	for i := 0; i < b.N; i++ {
		err = queue.RPush([]byte("test"))
		require.NoError(b, err)
	}
}

func BenchmarkPop(b *testing.B) {
	dbDir := b.TempDir()

	queue, err := Open(dbDir)
	require.NoError(b, err)
	b.StopTimer()
	for i := 0; i < b.N; i++ {
		err = queue.RPush([]byte("test"))
		require.NoError(b, err)
	}
	b.StartTimer()

	for i := 0; i < b.N; i++ {
		_, err = queue.RPop()
		require.NoError(b, err)
	}
}
