55 "os"
66 "path"
77 "path/filepath"
8+ "sync"
89
910 "github.com/linuxsuren/api-testing/pkg/util"
1011)
@@ -13,15 +14,17 @@ type fileLoader struct {
1314 paths []string
1415 index int
1516 parent string
17+
18+ lock * sync.RWMutex
1619}
1720
1821// NewFileLoader creates the instance of file loader
1922func NewFileLoader () Loader {
20- return & fileLoader {index : - 1 }
23+ return & fileLoader {index : - 1 , lock : & sync. RWMutex {} }
2124}
2225
2326func NewFileWriter (parent string ) Writer {
24- return & fileLoader {index : - 1 , parent : parent }
27+ return & fileLoader {index : - 1 , parent : parent , lock : & sync. RWMutex {} }
2528}
2629
2730// HasMore returns if there are more test cases
@@ -38,6 +41,9 @@ func (l *fileLoader) Load() (data []byte, err error) {
3841
3942// Put adds the test case path
4043func (l * fileLoader ) Put (item string ) (err error ) {
44+ l .lock .Lock ()
45+ defer l .lock .Unlock ()
46+
4147 if l .parent == "" {
4248 l .parent = path .Dir (item )
4349 }
@@ -47,9 +53,8 @@ func (l *fileLoader) Put(item string) (err error) {
4753 if files , err = filepath .Glob (pattern ); err == nil {
4854 l .paths = append (l .paths , files ... )
4955 }
50- fmt .Println (pattern , "pattern" , files )
56+ fmt .Println (pattern , "pattern" , len ( files ) )
5157 }
52- fmt .Println (l .paths , item , l .parent , err )
5358 return
5459}
5560
@@ -133,6 +138,9 @@ func (l *fileLoader) CreateSuite(name, api string) (err error) {
133138}
134139
135140func (l * fileLoader ) GetSuite (name string ) (suite * TestSuite , absPath string , err error ) {
141+ l .lock .RLock ()
142+ defer l .lock .RUnlock ()
143+
136144 for i := range l .paths {
137145 suitePath := l .paths [i ]
138146 if absPath , err = filepath .Abs (suitePath ); err != nil {
@@ -165,6 +173,9 @@ func (l *fileLoader) UpdateSuite(suite TestSuite) (err error) {
165173}
166174
167175func (l * fileLoader ) DeleteSuite (name string ) (err error ) {
176+ l .lock .Lock ()
177+ defer l .lock .Unlock ()
178+
168179 found := false
169180 for i := range l .paths {
170181 suitePath := l .paths [i ]
0 commit comments