test write

This commit is contained in:
Suyono 2024-06-26 18:33:25 +10:00
parent 95827a2efe
commit 9b94acad71

111
notify.go
View File

@ -15,6 +15,12 @@ import (
"github.com/fsnotify/fsnotify"
)
const (
createFile = iota
writeFile
deleteFile
)
func main() {
var (
watcher *fsnotify.Watcher
@ -42,10 +48,8 @@ func main() {
}
sm := new(sync.Map)
go createEmptyAndDelete(ctx, sm, time.Second, dir2Watch)
go createEmptyAndDelete(ctx, sm, time.Second+(time.Millisecond*200), dir2Watch)
go createEmptyAndDelete(ctx, sm, time.Second+(time.Millisecond*400), dir2Watch)
go createEmptyAndDelete(ctx, sm, time.Second+(time.Millisecond*600), dir2Watch)
go createEmptyAndDelete(ctx, sm, time.Second, time.Second, dir2Watch)
go createWriteAndDelete(ctx, sm, time.Second+(time.Millisecond*100), time.Millisecond*200, dir2Watch)
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGTERM)
@ -67,8 +71,8 @@ func watcherFunc(ctx context.Context, watcher *fsnotify.Watcher) {
}
log.Println("event:", event)
if event.Has(fsnotify.Create) {
log.Println("created file:", event.Name)
if event.Has(fsnotify.Write) {
log.Println("written to file:", event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
@ -126,24 +130,20 @@ func genRandomString(len uint) string {
return string(b)
}
func createEmptyAndDelete(ctx context.Context, sm *sync.Map, initialWait time.Duration, dir string) {
func createEmptyAndDelete(ctx context.Context, sm *sync.Map, initialWait time.Duration, iterWait time.Duration, dir string) {
if initialWait < 0 {
log.Println("initialWait cannot be less than 0")
return
}
if iterWait < 0 {
log.Println("iterWait cannot be less than 0")
}
tick := time.NewTicker(initialWait)
defer tick.Stop()
const (
createFile = iota
deleteFile
iterWait time.Duration = time.Second
)
var (
next int = createFile
next = createFile
nextFileName string
nextFile string
f *os.File
@ -157,8 +157,8 @@ func createEmptyAndDelete(ctx context.Context, sm *sync.Map, initialWait time.Du
select {
case <-tick.C:
tick.Stop()
switch next {
case createFile:
switch {
case next == createFile:
nextFileName = filepath.Join(dir, nextFileName)
log.Printf("creating file: %s\n", nextFileName)
if f, err = os.OpenFile(nextFileName, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666); err != nil {
@ -167,8 +167,81 @@ func createEmptyAndDelete(ctx context.Context, sm *sync.Map, initialWait time.Du
}
_ = f.Close()
next = deleteFile
case deleteFile:
log.Printf("deleting file: %s\n", nextFileName)
case next == deleteFile:
if err = os.Remove(nextFileName); err != nil {
log.Printf("Error deleting file %s: %+v\n", nextFileName, err)
return
}
next = createFile
sm.Delete(nextFile)
nextFile = genUniqueRandomString(sm, 6)
nextFileName = nextFile + ".txt"
}
tick.Reset(iterWait)
case <-ctx.Done():
if next == deleteFile {
_ = os.Remove(nextFileName)
}
return
}
}
}
func createWriteAndDelete(ctx context.Context, sm *sync.Map, initialWait time.Duration, iterWait time.Duration, dir string) {
if initialWait < 0 {
log.Println("initialWait cannot be less than 0")
return
}
if iterWait < 0 {
log.Println("iterWait cannot be less than 0")
}
tick := time.NewTicker(initialWait)
defer tick.Stop()
var (
next = createFile
nextFileName string
nextFile string
f *os.File
err error
currLen int64
wLen int
)
targetLen := rand.Int63n(1024*1024) + 102400
nextFile = genUniqueRandomString(sm, 6)
nextFileName = nextFile + ".txt"
for {
select {
case <-tick.C:
tick.Stop()
switch {
case next == createFile:
nextFileName = filepath.Join(dir, nextFileName)
log.Printf("creating file: %s\n", nextFileName)
if f, err = os.OpenFile(nextFileName, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666); err != nil {
log.Printf("Error creating file %s: %+v\n", nextFileName, err)
return
}
next = writeFile
case next == writeFile:
if wLen, err = f.WriteString(genRandomString(1024)); err != nil {
log.Printf("Error writing file %s: %+v\n", nextFileName, err)
_ = f.Close()
_ = os.Remove(nextFileName)
return
}
log.Printf("written %d bytes to %s\n", wLen, nextFileName)
currLen += int64(wLen)
if currLen > targetLen {
_ = f.Close()
next = deleteFile
}
case next == deleteFile:
if err = os.Remove(nextFileName); err != nil {
log.Printf("Error deleting file %s: %+v\n", nextFileName, err)
return