package system

import (
	"os"
	"path/filepath"
	"sync"
	"testing"
)

var wg = sync.WaitGroup{}

func TestAppendFileManager(t *testing.T) {
	defer func() {
		DestroyFileManager()
	}()

	path := "./io.go"
	absPath, err := filepath.Abs(path)
	if err != nil {
		t.Fatal(err)
	}

	_, err = os.Stat(absPath)
	if err != nil {
		t.Fatal(err)
	}

	f, err := os.OpenFile(absPath, os.O_RDONLY, 0644)
	if err != nil {
		t.Fatal(err)
	}
	FileManager.Set(f, absPath, os.O_RDONLY, 0644)

	t.Logf("文件数量: %d", FileManager.Len())

	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func(i int, t *testing.T) {
			f := FileManager.Get(absPath, os.O_RDONLY, 0644)
			if f != nil {
				t.Log(f.Stat())
			}

			if i == 5 {
				FileManager.Remove(absPath, os.O_RDONLY, 0644)
			}
			wg.Done()
		}(i, t)
	}

	wg.Wait()
}

func TestManager(t *testing.T) {
	path := "./io.go"
	absPath, err := filepath.Abs(path)
	if err != nil {
		t.Fatal(err)
	}

	_, err = os.Stat(absPath)
	if err != nil {
		t.Fatal(err)
	}

	f, err := os.OpenFile(absPath, os.O_RDONLY, 0644)
	if err != nil {
		t.Fatal(err)
	}

	FileManager.Set(f, absPath, os.O_RDONLY, 0644)

	f = FileManager.Get(absPath, 0, 0644)
	t.Log(f.Name())
}
