@@ -3,9 +3,12 @@ package afero
3
3
import (
4
4
"fmt"
5
5
"io"
6
+ "io/fs"
6
7
"os"
7
8
"path/filepath"
8
9
"runtime"
10
+ "strings"
11
+ "sync"
9
12
"testing"
10
13
"time"
11
14
)
@@ -692,3 +695,84 @@ func TestMemFsLstatIfPossible(t *testing.T) {
692
695
t .Fatalf ("Function indicated lstat was called. This should never be true." )
693
696
}
694
697
}
698
+
699
+ func TestMemMapFsConfurrentMkdir (t * testing.T ) {
700
+ const dir = "test_dir"
701
+ const n = 1000
702
+ mfs := NewMemMapFs ().(* MemMapFs )
703
+
704
+ allFilePaths := make ([]string , 0 , n )
705
+
706
+ // run concurrency test
707
+ var wg sync.WaitGroup
708
+ for i := 0 ; i < n ; i ++ {
709
+ fp := filepath .Join (
710
+ dir ,
711
+ fmt .Sprintf ("%02d" , n % 10 ),
712
+ fmt .Sprintf ("%d.txt" , i ),
713
+ )
714
+ allFilePaths = append (allFilePaths , fp )
715
+
716
+ wg .Add (1 )
717
+ go func () {
718
+ defer wg .Done ()
719
+
720
+ if err := mfs .MkdirAll (filepath .Dir (fp ), 0755 ); err != nil {
721
+ t .Error (err )
722
+ }
723
+
724
+ wt , err := mfs .Create (fp )
725
+ if err != nil {
726
+ t .Error (err )
727
+ }
728
+ defer func () {
729
+ if err := wt .Close (); err != nil {
730
+ t .Error (err )
731
+ }
732
+ }()
733
+
734
+ // write 30 bytes
735
+ for j := 0 ; j < 10 ; j ++ {
736
+ _ , err := wt .Write ([]byte ("000" ))
737
+ if err != nil {
738
+ t .Error (err )
739
+ }
740
+ }
741
+ }()
742
+ }
743
+ wg .Wait ()
744
+
745
+ // Test1: find all files by full path access
746
+ for _ , fp := range allFilePaths {
747
+ info , err := mfs .Stat (fp )
748
+ if err != nil {
749
+ t .Error (err )
750
+ }
751
+
752
+ if info .Size () != 30 {
753
+ t .Errorf ("file size should be 30, but got %d" , info .Size ())
754
+ }
755
+
756
+ }
757
+
758
+ // Test2: find all files by walk
759
+ foundFiles := make ([]string , 0 , n )
760
+ wErr := Walk (mfs , dir , func (path string , info fs.FileInfo , err error ) error {
761
+ if err != nil {
762
+ t .Error (err )
763
+ }
764
+ if info .IsDir () {
765
+ return nil // skip dir
766
+ }
767
+ if strings .HasSuffix (info .Name (), ".txt" ) {
768
+ foundFiles = append (foundFiles , path )
769
+ }
770
+ return nil
771
+ })
772
+ if wErr != nil {
773
+ t .Error (wErr )
774
+ }
775
+ if len (foundFiles ) != n {
776
+ t .Errorf ("found %d files, but expect %d" , len (foundFiles ), n )
777
+ }
778
+ }
0 commit comments