<commit_before>// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package fsnotify

import (
	"fmt"
	"math/rand"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"sync/atomic"
	"testing"
	"time"
)

var r *rand.Rand

func init() {
	r = rand.New(rand.NewSource(time.Now().UnixNano()))
}

func testTempDir() string {
	osTempDir := os.TempDir()
	randDir := fmt.Sprintf("%d", r.Int())
	return filepath.Join(osTempDir, randDir)
}

// An atomic counter
type counter struct {
	val int32
}

func (c *counter) increment() {
	cas := atomic.CompareAndSwapInt32
	old := atomic.LoadInt32(&c.val)
	for swp := cas(&c.val, old, old+1); !swp; swp = cas(&c.val, old, old+1) {
		old = atomic.LoadInt32(&c.val)
	}
}

func (c *counter) value() int32 {
	return atomic.LoadInt32(&c.val)
}

func TestFsnotifyMultipleOperations(t *testing.T) {
	// Create an fsnotify watcher instance and initialize it
	watcher, err := NewWatcher()
	if err != nil {
		t.Fatalf("NewWatcher() failed: %s", err)
	}

	// Receive errors on the error channel on a separate goroutine
	go func() {
		for err := range watcher.Error {
			t.Fatalf("error received: %s", err)
		}
	}()

	var testDir string = testTempDir()
	var testDirToMoveFiles string = testTempDir()

	// Create directory to watch
	if err := os.Mkdir(testDir, 0777); err != nil {
		t.Fatalf("failed to create test directory: %s", err)
	}
	defer os.RemoveAll(testDir)

	// Create directory to that's not watched
	if err := os.Mkdir(testDirToMoveFiles, 0777); err != nil {
		t.Fatalf("failed to create test directory: %s", err)
	}
	defer os.RemoveAll(testDirToMoveFiles)
<commit_msg>tests: Allow resetting the value of a counter<commit_after>// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package fsnotify

import (
	"fmt"
	"math/rand"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"sync/atomic"
	"testing"
	"time"
)

var r *rand.Rand

func init() {
	r = rand.New(rand.NewSource(time.Now().UnixNano()))
}

func testTempDir() string {
	osTempDir := os.TempDir()
	randDir := fmt.Sprintf("%d", r.Int())
	return filepath.Join(osTempDir, randDir)
}

// An atomic counter
type counter struct {
	val int32
}

func (c *counter) increment() {
	cas := atomic.CompareAndSwapInt32
	old := atomic.LoadInt32(&c.val)
	for swp := cas(&c.val, old, old+1); !swp; swp = cas(&c.val, old, old+1) {
		old = atomic.LoadInt32(&c.val)
	}
}

func (c *counter) value() int32 {
	return atomic.LoadInt32(&c.val)
}

func (c *counter) reset() {
	atomic.StoreInt32(&c.val, 0)
}

func TestFsnotifyMultipleOperations(t *testing.T) {
	// Create an fsnotify watcher instance and initialize it
	watcher, err := NewWatcher()
	if err != nil {
		t.Fatalf("NewWatcher() failed: %s", err)
	}

	// Receive errors on the error channel on a separate goroutine
	go func() {
		for err := range watcher.Error {
			t.Fatalf("error received: %s", err)
		}
	}()

	var testDir string = testTempDir()
	var testDirToMoveFiles string = testTempDir()

	// Create directory to watch
	if err := os.Mkdir(testDir, 0777); err != nil {
		t.Fatalf("failed to create test directory: %s", err)
	}
	defer os.RemoveAll(testDir)

	// Create directory to that's not watched
	if err := os.Mkdir(testDirToMoveFiles, 0777); err != nil {
		t.Fatalf("failed to create test directory: %s", err)
	}
	defer os.RemoveAll(testDirToMoveFiles)

{'fraction_non_alphanumeric': 0.14363376251788268, 'fraction_numerical': 0.015736766809728183, 'mean_word_length': 3.896358543417367, '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': '2174697', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1274, 'n_words': 377}