func TestSegment_openQueueSegment_failIfNew(t *testing.T) {
	testDir := "./TestSegment_Open"
	os.RemoveAll(testDir)
	if err := os.Mkdir(testDir, 0755); err != nil {
		t.Fatalf("Error creating directory in the TestSegment_Open method: %s\n", err)
	}
	seg, err := openQueueSegment(testDir, 1, false, item1Builder)
	if err == nil {
		t.Fatalf("openQueueSegment('%s') should have failed because it should be new\n", testDir)
	}
	assert(t, seg == nil, "segment after failure must be nil")
	if err := os.RemoveAll(testDir); err != nil {
		t.Fatalf("Error cleaning up directory from the TestSegment_Open method with '%s'\n", err.Error())
	}
}
TestSegment_Open verifies the behavior of the openSegment function.
package dque
import (
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"testing"
)
type item1 struct {
	Name string
}
func item1Builder() interface{} {
	return &item1{}
}
func TestSegment(t *testing.T) {
	testDir := "./TestSegment"
	os.RemoveAll(testDir)
	if err := os.Mkdir(testDir, 0755); err != nil {
		t.Fatalf("Error creating directory from the TestSegment method: %s\n", err)
	}
	seg, err := newQueueSegment(testDir, 1, false, item1Builder)
	if err != nil {
		t.Fatalf("newQueueSegment('%s') failed with '%s'\n", testDir, err.Error())
	}
	assert(t, seg.add(&item1{Name: "Number 1"}) == nil, "failed to add item1")
	assert(t, 1 == seg.size(), "Expected size of 1")
	assert(t, seg.add(&item1{Name: "Number 2"}) == nil, "failed to add item2")
	assert(t, 2 == seg.size(), "Expected size of 2")
	_, err = seg.remove()
	if err != nil {
		t.Fatalf("Remove() failed with '%s'\n", err.Error())
	}
	assert(t, 1 == seg.size(), "Expected size of 1")
	assert(t, 2 == seg.sizeOnDisk(), "Expected sizeOnDisk of 2")
	assert(t, seg.add(&item1{Name: "item3"}) == nil, "failed to add item3")
	assert(t, 2 == seg.size(), "Expected size of 2")
	_, err = seg.remove()
	if err != nil {
		t.Fatalf("Remove() failed with '%s'\n", err.Error())
	}
	assert(t, 1 == seg.size(), "Expected size of 1")
	seg, err = openQueueSegment(testDir, 1, false, item1Builder)
	if err != nil {
		t.Fatalf("openQueueSegment('%s') failed with '%s'\n", testDir, err.Error())
	}
	assert(t, 1 == seg.size(), "Expected size of 1")
	_, err = seg.remove()
	if err != nil {
		if err != errEmptySegment {
			t.Fatalf("Remove() failed with '%s'\n", err.Error())
		}
	}
	assert(t, 0 == seg.size(), "Expected size of 0")
	if err := os.RemoveAll(testDir); err != nil {
		t.Fatalf("Error cleaning up directory from the TestSegment method with '%s'\n", err.Error())
	}
}
func TestSegment_ErrCorruptedSegment(t *testing.T) {
	testDir := "./TestSegmentError"
	os.RemoveAll(testDir)
	defer os.RemoveAll((testDir))
	if err := os.Mkdir(testDir, 0755); err != nil {
		t.Fatalf("Error creating directory in the TestSegment_ErrCorruptedSegment method: %s\n", err)
	}
	f, err := os.Create((&qSegment{dirPath: testDir}).filePath())
	if err != nil {
		t.Fatal(err)
	}
	if _, err := f.Write([]byte{0, 0, 0, 8, 1, 2, 3, 4, 5, 6, 7}); err != nil {
		t.Fatal(err)
	}
	f.Close()
	_, err = openQueueSegment(testDir, 0, false, func() interface{} { return make([]byte, 8) })
	if err == nil {
		t.Fatal("expected ErrCorruptedSegment but got nil")
	}
	corruptedError, ok := unwrapError(unwrapError(err)).(ErrCorruptedSegment)
	if !ok {
		t.Fatalf("expected ErrCorruptedSegment but got %T: %s", err, err)
	}
	if corruptedError.Path != "TestSegmentError/0000000000000.dque" {
		t.Fatalf("unexpected file path: %s", corruptedError.Path)
	}
	if corruptedError.Error() != "segment file TestSegmentError/0000000000000.dque is corrupted: error reading gob data from file: unexpected EOF" {
		t.Fatalf("wrong error message: %s", corruptedError.Error())
	}
}
func unwrapError(err error) error {
	return err.(interface{ Unwrap() error }).Unwrap()
}