File: storagefolderadd_test.go

package info (click to toggle)
sia 1.3.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,340 kB
  • sloc: makefile: 80; sh: 52
file content (886 lines) | stat: -rw-r--r-- 27,358 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
package contractmanager

import (
	"errors"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"testing"
	"time"

	"github.com/NebulousLabs/Sia/modules"
)

// TestAddStorageFolder tries to add a storage folder to the contract manager,
// blocking until the add has completed.
func TestAddStorageFolder(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	cmt, err := newContractManagerTester("TestAddStorageFolder")
	if err != nil {
		t.Fatal(err)
	}
	defer cmt.panicClose()

	// Add a storage folder to the contract manager tester.
	storageFolderDir := filepath.Join(cmt.persistDir, "storageFolderOne")
	// Create the storage folder dir.
	err = os.MkdirAll(storageFolderDir, 0700)
	if err != nil {
		t.Fatal(err)
	}
	err = cmt.cm.AddStorageFolder(storageFolderDir, modules.SectorSize*storageFolderGranularity*2)
	if err != nil {
		t.Fatal(err)
	}

	// Check that the storage folder has been added.
	sfs := cmt.cm.StorageFolders()
	if len(sfs) != 1 {
		t.Fatal("There should be one storage folder reported")
	}
	// Check that the storage folder has the right path and size.
	if sfs[0].Path != storageFolderDir {
		t.Error("storage folder reported with wrong path")
	}
	if sfs[0].Capacity != modules.SectorSize*storageFolderGranularity*2 {
		t.Error("storage folder reported with wrong sector size")
	}
}

// dependencyLargeFolder is a mocked dependency that will return files which
// can only handle 1 MiB of data being written to them.
type dependencyLargeFolder struct {
	productionDependencies
}

// limitFile will return an error if a call to Write is made that will put the
// total throughput of the file over 1 MiB.
type limitFile struct {
	throughput int64
	mu         sync.Mutex
	*os.File
	sync.Mutex
}

// createFile will return a file that will return an error if a write will put
// the total throughput of the file over 1 MiB.
func (dependencyLargeFolder) createFile(s string) (file, error) {
	osFile, err := os.Create(s)
	if err != nil {
		return nil, err
	}

	lf := &limitFile{
		File: osFile,
	}
	return lf, nil
}

// Truncate returns an error if the operation will put the total throughput of
// the file over 8 MiB.
func (l *limitFile) Truncate(offset int64) error {
	l.mu.Lock()
	defer l.mu.Unlock()
	// If the limit has already been reached, return an error.
	if l.throughput >= 1<<20 {
		return errors.New("limitFile throughput limit reached earlier")
	}

	fi, err := l.Stat()
	if err != nil {
		return errors.New("limitFile could not fetch fileinfo: " + err.Error())
	}
	// No throughput if file is shrinking.
	if fi.Size() > offset {
		return l.File.Truncate(offset)
	}
	writeSize := offset - fi.Size()

	// If the limit has not been reached, pass the call through to the
	// underlying file. Limit counting is a little wonky because we assume the
	// file being passed in has currently a size of zero.
	if l.throughput+writeSize <= 1<<20 {
		l.throughput += writeSize
		return l.File.Truncate(offset)
	}

	// If the limit has been reached, return an error.
	return errors.New("limitFile throughput limit reached before all input was written to disk")
}

// TestAddLargeStorageFolder tries to add a storage folder that is too large to
// fit on disk. This is represented by mocking a file that returns an error
// after more than 8 MiB have been written.
func TestAddLargeStorageFolder(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	d := new(dependencyLargeFolder)
	cmt, err := newMockedContractManagerTester(d, "TestAddLargeStorageFolder")
	if err != nil {
		t.Fatal(err)
	}
	defer cmt.panicClose()

	// Add a storage folder to the contract manager tester.
	storageFolderDir := filepath.Join(cmt.persistDir, "storageFolderOne")
	// Create the storage folder dir.
	err = os.MkdirAll(storageFolderDir, 0700)
	if err != nil {
		t.Fatal(err)
	}
	addErr := cmt.cm.AddStorageFolder(storageFolderDir, modules.SectorSize*storageFolderGranularity*16) // Total size must exceed the limit of the limitFile.
	// Should be a storage folder error, but with all the context adding, I'm
	// not sure how to check the error type.
	if addErr == nil {
		t.Fatal(err)
	}

	// Check that the storage folder has been added.
	sfs := cmt.cm.StorageFolders()
	if len(sfs) != 0 {
		t.Fatal("Storage folder add should have failed.")
	}
	// Check that the storage folder is empty - because the operation failed,
	// any files that got created should have been removed.
	files, err := ioutil.ReadDir(storageFolderDir)
	if err != nil {
		t.Fatal(err)
	}
	if len(files) != 0 {
		t.Log(addErr)
		t.Error("there should not be any files in the storage folder because the AddStorageFolder operation failed.")
		t.Error(len(files))
		for _, file := range files {
			t.Error(file.Name())
		}
	}
}

// TestAddStorageFolderConcurrent adds multiple storage folders concurrently to
// the contract manager.
func TestAddStorageFolderConcurrent(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	cmt, err := newContractManagerTester("TestAddStorageFolderConcurrent")
	if err != nil {
		t.Fatal(err)
	}
	defer cmt.panicClose()

	// Add a storage folder to the contract manager tester.
	storageFolderOne := filepath.Join(cmt.persistDir, "storageFolderOne")
	storageFolderTwo := filepath.Join(cmt.persistDir, "storageFolderTwo")
	storageFolderThree := filepath.Join(cmt.persistDir, "storageFolderThree")
	// Create the storage folder dir.
	err = os.MkdirAll(storageFolderOne, 0700)
	if err != nil {
		t.Fatal(err)
	}
	err = os.MkdirAll(storageFolderTwo, 0700)
	if err != nil {
		t.Fatal(err)
	}
	err = os.MkdirAll(storageFolderThree, 0700)
	if err != nil {
		t.Fatal(err)
	}

	// Launch three calls to add simultaneously and wait for all three to
	// finish.
	var wg sync.WaitGroup
	wg.Add(3)
	go func() {
		defer wg.Done()
		err := cmt.cm.AddStorageFolder(storageFolderOne, modules.SectorSize*storageFolderGranularity*8)
		if err != nil {
			t.Fatal(err)
		}
	}()
	go func() {
		defer wg.Done()
		err := cmt.cm.AddStorageFolder(storageFolderTwo, modules.SectorSize*storageFolderGranularity*8)
		if err != nil {
			t.Fatal(err)
		}
	}()
	go func() {
		defer wg.Done()
		err = cmt.cm.AddStorageFolder(storageFolderThree, modules.SectorSize*storageFolderGranularity*8)
		if err != nil {
			t.Fatal(err)
		}
	}()
	wg.Wait()

	// Check that the storage folder has been added.
	sfs := cmt.cm.StorageFolders()
	if len(sfs) != 3 {
		t.Fatal("There should be one storage folder reported")
	}
}

// dependencyBlockSFOne is a mocked dependency for os.Create that will return a
// file for storage folder one only which will block on a call to file.Truncate
// until a signal has been given that the block can be released.
type dependencyBlockSFOne struct {
	blockLifted chan struct{}
	writeCalled chan struct{}
	productionDependencies
}

// blockedFile is the file that gets returned by dependencyBlockSFOne to
// storageFolderOne.
type blockedFile struct {
	blockLifted chan struct{}
	writeCalled chan struct{}
	*os.File
	sync.Mutex
}

// Truncate will block until a signal is given that the block may be lifted.
// Truncate will signal when it has been called for the first time, so that the
// tester knows the function has reached a blocking point.
func (bf *blockedFile) Truncate(offset int64) error {
	if !strings.Contains(bf.File.Name(), "storageFolderOne") || strings.Contains(bf.File.Name(), "siahostmetadata.dat") {
		return bf.File.Truncate(offset)
	}
	close(bf.writeCalled)
	<-bf.blockLifted
	return bf.File.Truncate(offset)
}

// createFile will return a normal file to all callers except for
// storageFolderOne, which will have calls to file.Write blocked until a signal
// is given that the blocks may be released.
func (d *dependencyBlockSFOne) createFile(s string) (file, error) {
	// If storageFolderOne, return a file that will not write until the signal
	// is sent that writing is okay.
	if strings.Contains(s, "storageFolderOne") {
		file, err := os.Create(s)
		if err != nil {
			return nil, err
		}
		bf := &blockedFile{
			blockLifted: d.blockLifted,
			writeCalled: d.writeCalled,
			File:        file,
		}
		return bf, nil
	}

	// If not storageFolderOne, return a normal file.
	return os.Create(s)
}

// TestAddStorageFolderBlocking adds multiple storage folders concurrently to
// the contract manager, blocking on the first one to make sure that the others
// are still allowed to complete.
func TestAddStorageFolderBlocking(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	// Create the mocked dependencies that will block for the first storage
	// folder.
	d := &dependencyBlockSFOne{
		blockLifted: make(chan struct{}),
		writeCalled: make(chan struct{}),
	}

	// Create a contract manager tester with the mocked dependencies.
	cmt, err := newMockedContractManagerTester(d, "TestAddStorageFolderBlocking")
	if err != nil {
		t.Fatal(err)
	}
	defer cmt.panicClose()

	// Add a storage folder to the contract manager tester.
	storageFolderOne := filepath.Join(cmt.persistDir, "storageFolderOne")
	storageFolderTwo := filepath.Join(cmt.persistDir, "storageFolderTwo")
	storageFolderThree := filepath.Join(cmt.persistDir, "storageFolderThree")
	// Create the storage folder dir.
	err = os.MkdirAll(storageFolderOne, 0700)
	if err != nil {
		t.Fatal(err)
	}
	err = os.MkdirAll(storageFolderTwo, 0700)
	if err != nil {
		t.Fatal(err)
	}
	err = os.MkdirAll(storageFolderThree, 0700)
	if err != nil {
		t.Fatal(err)
	}

	// Spin off the first goroutine, and then wait until write has been called
	// on the underlying file.
	sfOneSize := modules.SectorSize * storageFolderGranularity * 8
	go func() {
		err := cmt.cm.AddStorageFolder(storageFolderOne, sfOneSize)
		if err != nil {
			t.Fatal(err)
		}
	}()
	select {
	case <-time.After(time.Second * 5):
		t.Fatal("storage folder not written out")
	case <-d.writeCalled:
	}

	// Check the status of the storage folder. At this point, the folder should
	// be returned as an unfinished storage folder addition, with progress
	// indicating that the storage folder is at 0 bytes progressed out of
	// sfOneSize.
	sfs := cmt.cm.StorageFolders()
	if len(sfs) != 1 {
		t.Fatal("there should be one storage folder reported")
	}
	if sfs[0].ProgressNumerator != 0 {
		t.Error("storage folder is showing progress despite being blocked")
	}
	if sfs[0].ProgressDenominator != sfOneSize+sectorMetadataDiskSize*storageFolderGranularity*8 {
		t.Error("storage folder is not showing that an action is in progress, though one is", sfs[0].ProgressDenominator, sfOneSize)
	}

	var wg sync.WaitGroup
	wg.Add(2)
	go func() {
		defer wg.Done()
		err := cmt.cm.AddStorageFolder(storageFolderTwo, modules.SectorSize*storageFolderGranularity*8)
		if err != nil {
			t.Fatal(err)
		}
	}()
	go func() {
		defer wg.Done()
		err = cmt.cm.AddStorageFolder(storageFolderThree, modules.SectorSize*storageFolderGranularity*8)
		if err != nil {
			t.Fatal(err)
		}
	}()
	wg.Wait()
	close(d.blockLifted)
	cmt.cm.tg.Flush()

	// Check that the storage folder has been added.
	sfs = cmt.cm.StorageFolders()
	if len(sfs) != 3 {
		t.Fatal("There should be one storage folder reported")
	}
	// All actions should have completed, so all storage folders should be
	// reporting '0' in the progress denominator.
	for _, sf := range sfs {
		if sf.ProgressDenominator != 0 {
			t.Error("ProgressDenominator is indicating that actions still remain")
		}
	}
}

// TestAddStorageFolderConsecutive adds multiple storage folders consecutively
// to the contract manager, blocking on the first one to make sure that the
// others are still allowed to complete.
func TestAddStorageFolderConsecutive(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	// Create a contract manager tester with the mocked dependencies.
	cmt, err := newContractManagerTester("TestAddStorageFolderConsecutive")
	if err != nil {
		t.Fatal(err)
	}
	defer cmt.panicClose()

	// Add a storage folder to the contract manager tester.
	storageFolderOne := filepath.Join(cmt.persistDir, "storageFolderOne")
	storageFolderTwo := filepath.Join(cmt.persistDir, "storageFolderTwo")
	storageFolderThree := filepath.Join(cmt.persistDir, "storageFolderThree")
	// Create the storage folder dir.
	err = os.MkdirAll(storageFolderOne, 0700)
	if err != nil {
		t.Fatal(err)
	}
	err = os.MkdirAll(storageFolderTwo, 0700)
	if err != nil {
		t.Fatal(err)
	}
	err = os.MkdirAll(storageFolderThree, 0700)
	if err != nil {
		t.Fatal(err)
	}

	// Spin off the first goroutine, and then wait until write has been called
	// on the underlying file.
	sfSize := modules.SectorSize * storageFolderGranularity * 8
	err = cmt.cm.AddStorageFolder(storageFolderOne, sfSize)
	if err != nil {
		t.Fatal(err)
	}
	err = cmt.cm.AddStorageFolder(storageFolderTwo, sfSize)
	if err != nil {
		t.Fatal(err)
	}
	err = cmt.cm.AddStorageFolder(storageFolderThree, sfSize)
	if err != nil {
		t.Fatal(err)
	}

	// Check that the storage folder has been added.
	sfs := cmt.cm.StorageFolders()
	if len(sfs) != 3 {
		t.Fatal("There should be one storage folder reported")
	}
	// All actions should have completed, so all storage folders should be
	// reporting '0' in the progress denominator.
	for _, sf := range sfs {
		if sf.ProgressDenominator != 0 {
			t.Error("ProgressDenominator is indicating that actions still remain")
		}
	}
}

// TestAddStorageFolderDoubleAdd concurrently adds two storage
// folders with the same path to the contract manager.
func TestAddStorageFolderDoubleAdd(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	// Create a contract manager tester with the mocked dependencies.
	cmt, err := newContractManagerTester("TestAddStorageFolderDoubleAdd")
	if err != nil {
		t.Fatal(err)
	}
	defer cmt.panicClose()

	// Add a storage folder to the contract manager tester.
	storageFolderOne := filepath.Join(cmt.persistDir, "storageFolderOne")
	// Create the storage folder dir.
	err = os.MkdirAll(storageFolderOne, 0700)
	if err != nil {
		t.Fatal(err)
	}

	// Call AddStorageFolder in three separate goroutines, where the same path
	// is used in each. The errors are not checked because one of the storage
	// folders will succeed, but it's uncertain which one.
	sfSize := modules.SectorSize * storageFolderGranularity * 8
	err = cmt.cm.AddStorageFolder(storageFolderOne, sfSize)
	if err != nil {
		t.Fatal(err)
	}
	err = cmt.cm.AddStorageFolder(storageFolderOne, sfSize*2)
	if err != ErrRepeatFolder {
		t.Fatal(err)
	}

	// Check that the storage folder has been added.
	sfs := cmt.cm.StorageFolders()
	if len(sfs) != 1 {
		t.Fatal("There should be one storage folder reported")
	}
	// All actions should have completed, so all storage folders should be
	// reporting '0' in the progress denominator
	for _, sf := range sfs {
		if sf.ProgressDenominator != 0 {
			t.Error("ProgressDenominator is indicating that actions still remain")
		}
	}
}

// dependencyNoSyncLoop is a mocked dependency that will disable the sync loop.
type dependencyNoSyncLoop struct {
	productionDependencies
}

// disrupt will disrupt the threadedSyncLoop, causing the loop to terminate as
// soon as it is created.
func (dependencyNoSyncLoop) disrupt(s string) bool {
	if s == "threadedSyncLoopStart" || s == "cleanWALFile" {
		// Disrupt threadedSyncLoop. The sync loop will exit immediately
		// instead of executing commits. Also disrupt the process that removes
		// the WAL file following clean shutdown.
		return true
	}
	return false
}

// TestAddStorageFolderDoubleAddNoCommit hijacks the sync loop in the contract
// manager such that the sync loop will not run automatically. Then, without
// doing an actual commit, the test will indicate to open functions that a
// commit has completed, allowing the next storage folder operation to happen.
// Because the changes were finalized but not committed, extra code coverage
// should be achieved, though the result of the storage folder being rejected
// should be the same.
func TestAddStorageFolderDoubleAddNoCommit(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	d := new(dependencyNoSyncLoop)
	cmt, err := newMockedContractManagerTester(d, "TestAddStorageFolderDoubleAddNoCommit")
	if err != nil {
		t.Fatal(err)
	}
	// The closing of this channel must happen after the call to panicClose.
	closeFakeSyncChan := make(chan struct{})
	defer close(closeFakeSyncChan)
	defer cmt.panicClose()

	// The sync loop will never run, which means naively AddStorageFolder will
	// never return. To get AddStorageFolder to return before the commit
	// completes, spin up an alternate sync loop which only performs the
	// signaling responsibilities of the commit function.
	go func() {
		for {
			select {
			case <-closeFakeSyncChan:
				return
			case <-time.After(time.Millisecond * 250):
				// Signal that the commit operation has completed, even though
				// it has not.
				cmt.cm.wal.mu.Lock()
				close(cmt.cm.wal.syncChan)
				cmt.cm.wal.syncChan = make(chan struct{})
				cmt.cm.wal.mu.Unlock()
			}
		}
	}()

	// Add a storage folder to the contract manager tester.
	storageFolderOne := filepath.Join(cmt.persistDir, "storageFolderOne")
	// Create the storage folder dir.
	err = os.MkdirAll(storageFolderOne, 0700)
	if err != nil {
		t.Fatal(err)
	}

	// Call AddStorageFolder in three separate goroutines, where the same path
	// is used in each. The errors are not checked because one of the storage
	// folders will succeed, but it's uncertain which one.
	sfSize := modules.SectorSize * storageFolderGranularity * 8
	err = cmt.cm.AddStorageFolder(storageFolderOne, sfSize)
	if err != nil {
		t.Fatal(err)
	}
	err = cmt.cm.AddStorageFolder(storageFolderOne, sfSize*2)
	if err != ErrRepeatFolder {
		t.Fatal(err)
	}

	// Check that the storage folder has been added.
	sfs := cmt.cm.StorageFolders()
	if len(sfs) != 1 {
		t.Fatal("There should be one storage folder reported", len(sfs))
	}
	// All actions should have completed, so all storage folders should be
	// reporting '0' in the progress denominator
	for _, sf := range sfs {
		if sf.ProgressDenominator != 0 {
			t.Error("ProgressDenominator is indicating that actions still remain")
		}
	}
}

// TestAddStorageFolderFailedCommit adds a storage folder without ever saving
// the settings.
func TestAddStorageFolderFailedCommit(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	d := new(dependencyNoSettingsSave)
	cmt, err := newMockedContractManagerTester(d, "TestAddStorageFolderFailedCommit")
	if err != nil {
		t.Fatal(err)
	}
	defer cmt.panicClose()

	// Add a storage folder to the contract manager tester.
	storageFolderOne := filepath.Join(cmt.persistDir, "storageFolderOne")
	// Create the storage folder dir.
	err = os.MkdirAll(storageFolderOne, 0700)
	if err != nil {
		t.Fatal(err)
	}
	sfSize := modules.SectorSize * storageFolderGranularity * 8
	err = cmt.cm.AddStorageFolder(storageFolderOne, sfSize)
	if err != nil {
		t.Fatal(err)
	}
	d.mu.Lock()
	d.triggered = true
	d.mu.Unlock()

	// Check that the storage folder has been added.
	sfs := cmt.cm.StorageFolders()
	if len(sfs) != 1 {
		t.Fatal("There should be one storage folder reported")
	}
	// All actions should have completed, so all storage folders should be
	// reporting '0' in the progress denominator
	if sfs[0].ProgressDenominator != 0 {
		t.Error("ProgressDenominator is indicating that actions still remain")
	}

	// Close the contract manager and replace it with a new contract manager.
	// The new contract manager should have normal dependencies.
	err = cmt.cm.Close()
	if err != nil {
		t.Fatal(err)
	}
	// Create the new contract manager using the same persist dir, so that it
	// will see the uncommitted WAL.
	cmt.cm, err = New(filepath.Join(cmt.persistDir, modules.ContractManagerDir))
	if err != nil {
		t.Fatal(err)
	}
	// Check that the storage folder was properly recovered.
	sfs = cmt.cm.StorageFolders()
	if len(sfs) != 1 {
		t.Fatal("There should be one storage folder reported", len(sfs))
	}
}

// dependencySFAddNoFinish is a mocked dependency that will prevent the
type dependencySFAddNoFinish struct {
	productionDependencies
}

// disrupt will disrupt the threadedSyncLoop, causing the loop to terminate as
// soon as it is created.
func (d *dependencySFAddNoFinish) disrupt(s string) bool {
	if s == "storageFolderAddFinish" {
		return true
	}
	if s == "cleanWALFile" {
		// Prevent the WAL file from being removed.
		return true
	}
	return false
}

// TestAddStorageFolderUnfinishedCreate hijacks both the sync loop and the
// AddStorageFolder code to create a situation where the added storage folder
// is started but not seen through to conclusion, and no commit is run.
func TestAddStorageFolderUnfinishedCreate(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	d := new(dependencySFAddNoFinish)
	cmt, err := newMockedContractManagerTester(d, "TestAddStorageFolderUnfinishedCreate")
	if err != nil {
		t.Fatal(err)
	}
	defer cmt.panicClose()

	// Add a storage folder to the contract manager tester.
	storageFolderOne := filepath.Join(cmt.persistDir, "storageFolderOne")
	// Create the storage folder dir.
	err = os.MkdirAll(storageFolderOne, 0700)
	if err != nil {
		t.Fatal(err)
	}
	// Call AddStorageFolder, knowing that the changes will not be properly
	// committed, and that the call itself will not actually complete.
	sfSize := modules.SectorSize * storageFolderGranularity * 8
	err = cmt.cm.AddStorageFolder(storageFolderOne, sfSize)
	if err != nil {
		t.Fatal(err)
	}

	// Check that the storage folder has been added.
	sfs := cmt.cm.StorageFolders()
	if len(sfs) != 1 {
		t.Fatal("There should be one storage folder reported")
	}

	// Close the contract manager and replace it with a new contract manager.
	// The new contract manager should have normal dependencies.
	err = cmt.cm.Close()
	if err != nil {
		t.Fatal(err)
	}
	// Create the new contract manager using the same persist dir, so that it
	// will see the uncommitted WAL.
	cmt.cm, err = New(filepath.Join(cmt.persistDir, modules.ContractManagerDir))
	if err != nil {
		t.Fatal(err)
	}
	// Check that the storage folder was properly removed - incomplete storage
	// folder adds should be removed upon startup.
	sfs = cmt.cm.StorageFolders()
	if len(sfs) != 0 {
		t.Error("Storage folder add should have failed.")
	}
	// Check that the storage folder is empty - because the operation failed,
	// any files that got created should have been removed.
	files, err := ioutil.ReadDir(storageFolderOne)
	if err != nil {
		t.Error(err)
	}
	if len(files) != 0 {
		t.Error("there should not be any files in the storage folder because the AddStorageFolder operation failed:", len(files))
		t.Error(len(files))
		for _, file := range files {
			t.Error(file.Name())
		}
	}
}

// TestAddStorageFolderDoubleAddConcurrent concurrently adds two storage
// folders with the same path to the contract manager.
func TestAddStorageFolderDoubleAddConcurrent(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	// Create a contract manager tester with the mocked dependencies.
	cmt, err := newContractManagerTester("TestAddStorageFolderDoubleAddConcurrent")
	if err != nil {
		t.Fatal(err)
	}
	defer cmt.panicClose()

	// Add a storage folder to the contract manager tester.
	storageFolderOne := filepath.Join(cmt.persistDir, "storageFolderOne")
	// Create the storage folder dir.
	err = os.MkdirAll(storageFolderOne, 0700)
	if err != nil {
		t.Fatal(err)
	}

	// Call AddStorageFolder in three separate goroutines, where the same path
	// is used in each. The errors are not checked because one of the storage
	// folders will succeed, but it's uncertain which one.
	var wg sync.WaitGroup
	sfSize := modules.SectorSize * storageFolderGranularity * 8
	wg.Add(3)
	go func() {
		_ = cmt.cm.AddStorageFolder(storageFolderOne, sfSize)
		wg.Done()
	}()
	go func() {
		_ = cmt.cm.AddStorageFolder(storageFolderOne, sfSize*2)
		wg.Done()
	}()
	go func() {
		_ = cmt.cm.AddStorageFolder(storageFolderOne, sfSize*3)
		wg.Done()
	}()
	wg.Wait()

	// Check that the storage folder has been added.
	sfs := cmt.cm.StorageFolders()
	if len(sfs) != 1 {
		t.Fatal("There should be one storage folder reported")
	}
	// All actions should have completed, so all storage folders should be
	// reporting '0' in the progress denominator.
	for _, sf := range sfs {
		if sf.ProgressDenominator != 0 {
			t.Error("ProgressDenominator is indicating that actions still remain")
		}
	}
}

// TestAddStorageFolderReload adds a storage folder to the contract manager,
// and then reloads the contract manager to see if the storage folder is still
// there.
func TestAddStorageFolderReload(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	// Create a contract manager tester with the mocked dependencies.
	cmt, err := newContractManagerTester("TestAddStorageFolderReload")
	if err != nil {
		t.Fatal(err)
	}
	defer cmt.panicClose()

	// Add a storage folder to the contract manager tester.
	storageFolderOne := filepath.Join(cmt.persistDir, "storageFolderOne")
	// Create the storage folder dir.
	err = os.MkdirAll(storageFolderOne, 0700)
	if err != nil {
		t.Fatal(err)
	}
	sfSize := modules.SectorSize * storageFolderGranularity * 24
	err = cmt.cm.AddStorageFolder(storageFolderOne, sfSize)
	if err != nil {
		t.Fatal(err)
	}

	// Check that the storage folder has been added.
	sfs := cmt.cm.StorageFolders()
	if len(sfs) != 1 {
		t.Fatal("There should be one storage folder reported")
	}
	// Check that the size of the storage folder is correct.
	if sfs[0].Capacity != sfSize {
		t.Error("capacity reported by storage folder is not the capacity alloacted")
	}
	if sfs[0].CapacityRemaining != sfSize {
		t.Error("capacity remaining reported by storage folder is not the capacity alloacted")
	}
	// All actions should have completed, so all storage folders should be
	// reporting '0' in the progress denominator.
	for _, sf := range sfs {
		if sf.ProgressDenominator != 0 {
			t.Error("ProgressDenominator is indicating that actions still remain")
		}
	}

	// Close the contract manager and open a new one using the same
	// persistence.
	err = cmt.cm.Close()
	if err != nil {
		t.Fatal(err)
	}
	cmt.cm, err = New(filepath.Join(cmt.persistDir, modules.ContractManagerDir))
	if err != nil {
		t.Fatal(err)
	}

	// Check that the storage folder has been added.
	sfs = cmt.cm.StorageFolders()
	if len(sfs) != 1 {
		t.Fatal("There should be one storage folder reported", len(sfs))
	}
	// Check that the size of the storage folder is correct.
	if sfs[0].Capacity != sfSize {
		t.Error("capacity reported by storage folder is not the capacity alloacted")
	}
	if sfs[0].CapacityRemaining != sfSize {
		t.Error("capacity remaining reported by storage folder is not the capacity alloacted", sfs[0].Capacity, sfs[0].CapacityRemaining)
	}
	// Check that the storage folder as represented on disk has the correct
	// size.
	sectorLookupTableSize := int64(storageFolderGranularity * 24 * sectorMetadataDiskSize)
	expectedSize := int64(sfSize)
	fi, err := os.Stat(filepath.Join(storageFolderOne, sectorFile))
	if err != nil {
		t.Fatal(err)
	}
	if fi.Size() != expectedSize {
		t.Error("sector file had unexpected size", fi.Size(), expectedSize)
	}
	fi, err = os.Stat(filepath.Join(storageFolderOne, metadataFile))
	if err != nil {
		t.Fatal(err)
	}
	if fi.Size() != sectorLookupTableSize {
		t.Error("sector file had unexpected size", fi.Size(), sectorLookupTableSize)
	}
}