This file is indexed.

/usr/share/gocode/src/github.com/jacobsa/fuse/samples/cachingfs/caching_fs_test.go is in golang-github-jacobsa-fuse-dev 0.0~git20150806.0.9a7512a-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cachingfs_test

import (
	"bytes"
	"io/ioutil"
	"os"
	"path"
	"runtime"
	"syscall"
	"testing"
	"time"

	"github.com/jacobsa/fuse/fuseutil"
	"github.com/jacobsa/fuse/samples"
	"github.com/jacobsa/fuse/samples/cachingfs"
	. "github.com/jacobsa/oglematchers"
	. "github.com/jacobsa/ogletest"
	"github.com/jacobsa/timeutil"
)

func TestCachingFS(t *testing.T) { RunTests(t) }

////////////////////////////////////////////////////////////////////////
// Boilerplate
////////////////////////////////////////////////////////////////////////

type cachingFSTest struct {
	samples.SampleTest

	fs           cachingfs.CachingFS
	initialMtime time.Time
}

var _ TearDownInterface = &cachingFSTest{}

func (t *cachingFSTest) setUp(
	ti *TestInfo,
	lookupEntryTimeout time.Duration,
	getattrTimeout time.Duration) {
	var err error

	// Create the file system.
	t.fs, err = cachingfs.NewCachingFS(lookupEntryTimeout, getattrTimeout)
	AssertEq(nil, err)

	t.Server = fuseutil.NewFileSystemServer(t.fs)

	// Mount it.
	t.SampleTest.SetUp(ti)

	// Set up the mtime.
	t.initialMtime = time.Date(2012, 8, 15, 22, 56, 0, 0, time.Local)
	t.fs.SetMtime(t.initialMtime)
}

func (t *cachingFSTest) statAll() (foo, dir, bar os.FileInfo) {
	var err error

	foo, err = os.Stat(path.Join(t.Dir, "foo"))
	AssertEq(nil, err)

	dir, err = os.Stat(path.Join(t.Dir, "dir"))
	AssertEq(nil, err)

	bar, err = os.Stat(path.Join(t.Dir, "dir/bar"))
	AssertEq(nil, err)

	return
}

func (t *cachingFSTest) openFiles() (foo, dir, bar *os.File) {
	var err error

	foo, err = os.Open(path.Join(t.Dir, "foo"))
	AssertEq(nil, err)

	dir, err = os.Open(path.Join(t.Dir, "dir"))
	AssertEq(nil, err)

	bar, err = os.Open(path.Join(t.Dir, "dir/bar"))
	AssertEq(nil, err)

	return
}

func (t *cachingFSTest) statFiles(
	f, g, h *os.File) (foo, dir, bar os.FileInfo) {
	var err error

	foo, err = f.Stat()
	AssertEq(nil, err)

	dir, err = g.Stat()
	AssertEq(nil, err)

	bar, err = h.Stat()
	AssertEq(nil, err)

	return
}

func getInodeID(fi os.FileInfo) uint64 {
	return fi.Sys().(*syscall.Stat_t).Ino
}

////////////////////////////////////////////////////////////////////////
// Basics
////////////////////////////////////////////////////////////////////////

type BasicsTest struct {
	cachingFSTest
}

var _ SetUpInterface = &BasicsTest{}

func init() { RegisterTestSuite(&BasicsTest{}) }

func (t *BasicsTest) SetUp(ti *TestInfo) {
	const (
		lookupEntryTimeout = 0
		getattrTimeout     = 0
	)

	t.cachingFSTest.setUp(ti, lookupEntryTimeout, getattrTimeout)
}

func (t *BasicsTest) StatNonexistent() {
	names := []string{
		"blah",
		"bar",
		"dir/blah",
		"dir/dir",
		"dir/foo",
	}

	for _, n := range names {
		_, err := os.Stat(path.Join(t.Dir, n))

		AssertNe(nil, err)
		ExpectTrue(os.IsNotExist(err), "n: %s, err: %v", n, err)
	}
}

func (t *BasicsTest) StatFoo() {
	fi, err := os.Stat(path.Join(t.Dir, "foo"))
	AssertEq(nil, err)

	ExpectEq("foo", fi.Name())
	ExpectEq(cachingfs.FooSize, fi.Size())
	ExpectEq(0777, fi.Mode())
	ExpectThat(fi.ModTime(), timeutil.TimeEq(t.initialMtime))
	ExpectFalse(fi.IsDir())
	ExpectEq(t.fs.FooID(), getInodeID(fi))
	ExpectEq(1, fi.Sys().(*syscall.Stat_t).Nlink)
}

func (t *BasicsTest) StatDir() {
	fi, err := os.Stat(path.Join(t.Dir, "dir"))
	AssertEq(nil, err)

	ExpectEq("dir", fi.Name())
	ExpectEq(os.ModeDir|0777, fi.Mode())
	ExpectThat(fi.ModTime(), timeutil.TimeEq(t.initialMtime))
	ExpectTrue(fi.IsDir())
	ExpectEq(t.fs.DirID(), getInodeID(fi))
	ExpectEq(1, fi.Sys().(*syscall.Stat_t).Nlink)
}

func (t *BasicsTest) StatBar() {
	fi, err := os.Stat(path.Join(t.Dir, "dir/bar"))
	AssertEq(nil, err)

	ExpectEq("bar", fi.Name())
	ExpectEq(cachingfs.BarSize, fi.Size())
	ExpectEq(0777, fi.Mode())
	ExpectThat(fi.ModTime(), timeutil.TimeEq(t.initialMtime))
	ExpectFalse(fi.IsDir())
	ExpectEq(t.fs.BarID(), getInodeID(fi))
	ExpectEq(1, fi.Sys().(*syscall.Stat_t).Nlink)
}

////////////////////////////////////////////////////////////////////////
// No caching
////////////////////////////////////////////////////////////////////////

type NoCachingTest struct {
	cachingFSTest
}

var _ SetUpInterface = &NoCachingTest{}

func init() { RegisterTestSuite(&NoCachingTest{}) }

func (t *NoCachingTest) SetUp(ti *TestInfo) {
	const (
		lookupEntryTimeout = 0
		getattrTimeout     = 0
	)

	t.cachingFSTest.setUp(ti, lookupEntryTimeout, getattrTimeout)
}

func (t *NoCachingTest) StatStat() {
	fooBefore, dirBefore, barBefore := t.statAll()
	fooAfter, dirAfter, barAfter := t.statAll()

	// Make sure everything matches.
	ExpectThat(fooAfter.ModTime(), timeutil.TimeEq(fooBefore.ModTime()))
	ExpectThat(dirAfter.ModTime(), timeutil.TimeEq(dirBefore.ModTime()))
	ExpectThat(barAfter.ModTime(), timeutil.TimeEq(barBefore.ModTime()))

	ExpectEq(getInodeID(fooBefore), getInodeID(fooAfter))
	ExpectEq(getInodeID(dirBefore), getInodeID(dirAfter))
	ExpectEq(getInodeID(barBefore), getInodeID(barAfter))
}

func (t *NoCachingTest) StatRenumberStat() {
	t.statAll()
	t.fs.RenumberInodes()
	fooAfter, dirAfter, barAfter := t.statAll()

	// We should see the new inode IDs, because the entries should not have been
	// cached.
	ExpectEq(t.fs.FooID(), getInodeID(fooAfter))
	ExpectEq(t.fs.DirID(), getInodeID(dirAfter))
	ExpectEq(t.fs.BarID(), getInodeID(barAfter))
}

func (t *NoCachingTest) StatMtimeStat() {
	newMtime := t.initialMtime.Add(time.Second)

	t.statAll()
	t.fs.SetMtime(newMtime)
	fooAfter, dirAfter, barAfter := t.statAll()

	// We should see the new mtimes, because the attributes should not have been
	// cached.
	ExpectThat(fooAfter.ModTime(), timeutil.TimeEq(newMtime))
	ExpectThat(dirAfter.ModTime(), timeutil.TimeEq(newMtime))
	ExpectThat(barAfter.ModTime(), timeutil.TimeEq(newMtime))
}

func (t *NoCachingTest) StatRenumberMtimeStat() {
	newMtime := t.initialMtime.Add(time.Second)

	t.statAll()
	t.fs.RenumberInodes()
	t.fs.SetMtime(newMtime)
	fooAfter, dirAfter, barAfter := t.statAll()

	// We should see the new inode IDs and mtimes, because nothing should have
	// been cached.
	ExpectEq(t.fs.FooID(), getInodeID(fooAfter))
	ExpectEq(t.fs.DirID(), getInodeID(dirAfter))
	ExpectEq(t.fs.BarID(), getInodeID(barAfter))

	ExpectThat(fooAfter.ModTime(), timeutil.TimeEq(newMtime))
	ExpectThat(dirAfter.ModTime(), timeutil.TimeEq(newMtime))
	ExpectThat(barAfter.ModTime(), timeutil.TimeEq(newMtime))
}

////////////////////////////////////////////////////////////////////////
// Entry caching
////////////////////////////////////////////////////////////////////////

type EntryCachingTest struct {
	cachingFSTest
	lookupEntryTimeout time.Duration
}

var _ SetUpInterface = &EntryCachingTest{}

func init() { RegisterTestSuite(&EntryCachingTest{}) }

func (t *EntryCachingTest) SetUp(ti *TestInfo) {
	t.lookupEntryTimeout = 250 * time.Millisecond
	t.SampleTest.MountConfig.EnableVnodeCaching = true

	t.cachingFSTest.setUp(ti, t.lookupEntryTimeout, 0)
}

func (t *EntryCachingTest) StatStat() {
	fooBefore, dirBefore, barBefore := t.statAll()
	fooAfter, dirAfter, barAfter := t.statAll()

	// Make sure everything matches.
	ExpectThat(fooAfter.ModTime(), timeutil.TimeEq(fooBefore.ModTime()))
	ExpectThat(dirAfter.ModTime(), timeutil.TimeEq(dirBefore.ModTime()))
	ExpectThat(barAfter.ModTime(), timeutil.TimeEq(barBefore.ModTime()))

	ExpectEq(getInodeID(fooBefore), getInodeID(fooAfter))
	ExpectEq(getInodeID(dirBefore), getInodeID(dirAfter))
	ExpectEq(getInodeID(barBefore), getInodeID(barAfter))
}

func (t *EntryCachingTest) StatRenumberStat() {
	fooBefore, dirBefore, barBefore := t.statAll()
	t.fs.RenumberInodes()
	fooAfter, dirAfter, barAfter := t.statAll()

	// We should still see the old inode IDs, because the inode entries should
	// have been cached.
	ExpectEq(getInodeID(fooBefore), getInodeID(fooAfter))
	ExpectEq(getInodeID(dirBefore), getInodeID(dirAfter))
	ExpectEq(getInodeID(barBefore), getInodeID(barAfter))

	// But after waiting for the entry cache to expire, we should see the new
	// IDs.
	//
	// Note that the cache is not guaranteed to expire on darwin. See notes on
	// fuse.MountConfig.EnableVnodeCaching.
	if runtime.GOOS != "darwin" {
		time.Sleep(2 * t.lookupEntryTimeout)
		fooAfter, dirAfter, barAfter = t.statAll()

		ExpectEq(t.fs.FooID(), getInodeID(fooAfter))
		ExpectEq(t.fs.DirID(), getInodeID(dirAfter))
		ExpectEq(t.fs.BarID(), getInodeID(barAfter))
	}
}

func (t *EntryCachingTest) StatMtimeStat() {
	newMtime := t.initialMtime.Add(time.Second)

	t.statAll()
	t.fs.SetMtime(newMtime)
	fooAfter, dirAfter, barAfter := t.statAll()

	// We should see the new mtimes, because the attributes should not have been
	// cached.
	ExpectThat(fooAfter.ModTime(), timeutil.TimeEq(newMtime))
	ExpectThat(dirAfter.ModTime(), timeutil.TimeEq(newMtime))
	ExpectThat(barAfter.ModTime(), timeutil.TimeEq(newMtime))
}

func (t *EntryCachingTest) StatRenumberMtimeStat() {
	newMtime := t.initialMtime.Add(time.Second)

	fooBefore, dirBefore, barBefore := t.statAll()
	t.fs.RenumberInodes()
	t.fs.SetMtime(newMtime)
	fooAfter, dirAfter, barAfter := t.statAll()

	// We should still see the old inode IDs, because the inode entries should
	// have been cached. But the attributes should not have been.
	ExpectEq(getInodeID(fooBefore), getInodeID(fooAfter))
	ExpectEq(getInodeID(dirBefore), getInodeID(dirAfter))
	ExpectEq(getInodeID(barBefore), getInodeID(barAfter))

	ExpectThat(fooAfter.ModTime(), timeutil.TimeEq(newMtime))
	ExpectThat(dirAfter.ModTime(), timeutil.TimeEq(newMtime))
	ExpectThat(barAfter.ModTime(), timeutil.TimeEq(newMtime))

	// After waiting for the entry cache to expire, we should see fresh
	// everything.
	//
	// Note that the cache is not guaranteed to expire on darwin. See notes on
	// fuse.MountConfig.EnableVnodeCaching.
	if runtime.GOOS != "darwin" {
		time.Sleep(2 * t.lookupEntryTimeout)
		fooAfter, dirAfter, barAfter = t.statAll()

		ExpectEq(t.fs.FooID(), getInodeID(fooAfter))
		ExpectEq(t.fs.DirID(), getInodeID(dirAfter))
		ExpectEq(t.fs.BarID(), getInodeID(barAfter))

		ExpectThat(fooAfter.ModTime(), timeutil.TimeEq(newMtime))
		ExpectThat(dirAfter.ModTime(), timeutil.TimeEq(newMtime))
		ExpectThat(barAfter.ModTime(), timeutil.TimeEq(newMtime))
	}
}

////////////////////////////////////////////////////////////////////////
// Attribute caching
////////////////////////////////////////////////////////////////////////

type AttributeCachingTest struct {
	cachingFSTest
	getattrTimeout time.Duration
}

var _ SetUpInterface = &AttributeCachingTest{}

func init() { RegisterTestSuite(&AttributeCachingTest{}) }

func (t *AttributeCachingTest) SetUp(ti *TestInfo) {
	t.getattrTimeout = 250 * time.Millisecond
	t.cachingFSTest.setUp(ti, 0, t.getattrTimeout)
}

func (t *AttributeCachingTest) StatStat() {
	fooBefore, dirBefore, barBefore := t.statAll()
	fooAfter, dirAfter, barAfter := t.statAll()

	// Make sure everything matches.
	ExpectThat(fooAfter.ModTime(), timeutil.TimeEq(fooBefore.ModTime()))
	ExpectThat(dirAfter.ModTime(), timeutil.TimeEq(dirBefore.ModTime()))
	ExpectThat(barAfter.ModTime(), timeutil.TimeEq(barBefore.ModTime()))

	ExpectEq(getInodeID(fooBefore), getInodeID(fooAfter))
	ExpectEq(getInodeID(dirBefore), getInodeID(dirAfter))
	ExpectEq(getInodeID(barBefore), getInodeID(barAfter))
}

func (t *AttributeCachingTest) StatRenumberStat() {
	t.statAll()
	t.fs.RenumberInodes()
	fooAfter, dirAfter, barAfter := t.statAll()

	// We should see the new inode IDs, because the entries should not have been
	// cached.
	ExpectEq(t.fs.FooID(), getInodeID(fooAfter))
	ExpectEq(t.fs.DirID(), getInodeID(dirAfter))
	ExpectEq(t.fs.BarID(), getInodeID(barAfter))
}

func (t *AttributeCachingTest) StatMtimeStat_ViaPath() {
	newMtime := t.initialMtime.Add(time.Second)

	t.statAll()
	t.fs.SetMtime(newMtime)
	fooAfter, dirAfter, barAfter := t.statAll()

	// Since we don't have entry caching enabled, the call above had to look up
	// the entry again. With the lookup we returned new attributes, so it's
	// possible that the mtime will be fresh. On Linux it appears to be, and on
	// OS X it appears to not be.
	m := AnyOf(timeutil.TimeEq(newMtime), timeutil.TimeEq(t.initialMtime))
	ExpectThat(fooAfter.ModTime(), m)
	ExpectThat(dirAfter.ModTime(), m)
	ExpectThat(barAfter.ModTime(), m)
}

func (t *AttributeCachingTest) StatMtimeStat_ViaFileDescriptor() {
	newMtime := t.initialMtime.Add(time.Second)

	// Open everything, fixing a particular inode number for each.
	foo, dir, bar := t.openFiles()
	defer func() {
		foo.Close()
		dir.Close()
		bar.Close()
	}()

	fooBefore, dirBefore, barBefore := t.statFiles(foo, dir, bar)
	t.fs.SetMtime(newMtime)
	fooAfter, dirAfter, barAfter := t.statFiles(foo, dir, bar)

	// We should still see the old cached mtime.
	ExpectThat(fooAfter.ModTime(), timeutil.TimeEq(fooBefore.ModTime()))
	ExpectThat(dirAfter.ModTime(), timeutil.TimeEq(dirBefore.ModTime()))
	ExpectThat(barAfter.ModTime(), timeutil.TimeEq(barBefore.ModTime()))

	// After waiting for the attribute cache to expire, we should see the fresh
	// mtime.
	time.Sleep(2 * t.getattrTimeout)
	fooAfter, dirAfter, barAfter = t.statFiles(foo, dir, bar)

	ExpectThat(fooAfter.ModTime(), timeutil.TimeEq(newMtime))
	ExpectThat(dirAfter.ModTime(), timeutil.TimeEq(newMtime))
	ExpectThat(barAfter.ModTime(), timeutil.TimeEq(newMtime))
}

func (t *AttributeCachingTest) StatRenumberMtimeStat_ViaPath() {
	newMtime := t.initialMtime.Add(time.Second)

	t.statAll()
	t.fs.RenumberInodes()
	t.fs.SetMtime(newMtime)
	fooAfter, dirAfter, barAfter := t.statAll()

	// We should see new everything, because this is the first time the new
	// inodes have been encountered. Entries for the old ones should not have
	// been cached, because we have entry caching disabled.
	ExpectEq(t.fs.FooID(), getInodeID(fooAfter))
	ExpectEq(t.fs.DirID(), getInodeID(dirAfter))
	ExpectEq(t.fs.BarID(), getInodeID(barAfter))

	ExpectThat(fooAfter.ModTime(), timeutil.TimeEq(newMtime))
	ExpectThat(dirAfter.ModTime(), timeutil.TimeEq(newMtime))
	ExpectThat(barAfter.ModTime(), timeutil.TimeEq(newMtime))
}

func (t *AttributeCachingTest) StatRenumberMtimeStat_ViaFileDescriptor() {
	newMtime := t.initialMtime.Add(time.Second)

	// Open everything, fixing a particular inode number for each.
	foo, dir, bar := t.openFiles()
	defer func() {
		foo.Close()
		dir.Close()
		bar.Close()
	}()

	fooBefore, dirBefore, barBefore := t.statFiles(foo, dir, bar)
	t.fs.RenumberInodes()
	t.fs.SetMtime(newMtime)
	fooAfter, dirAfter, barAfter := t.statFiles(foo, dir, bar)

	// We should still see the old cached mtime with the old inode ID.
	ExpectEq(getInodeID(fooBefore), getInodeID(fooAfter))
	ExpectEq(getInodeID(dirBefore), getInodeID(dirAfter))
	ExpectEq(getInodeID(barBefore), getInodeID(barAfter))

	ExpectThat(fooAfter.ModTime(), timeutil.TimeEq(fooBefore.ModTime()))
	ExpectThat(dirAfter.ModTime(), timeutil.TimeEq(dirBefore.ModTime()))
	ExpectThat(barAfter.ModTime(), timeutil.TimeEq(barBefore.ModTime()))

	// After waiting for the attribute cache to expire, we should see the fresh
	// mtime, still with the old inode ID.
	time.Sleep(2 * t.getattrTimeout)
	fooAfter, dirAfter, barAfter = t.statFiles(foo, dir, bar)

	ExpectEq(getInodeID(fooBefore), getInodeID(fooAfter))
	ExpectEq(getInodeID(dirBefore), getInodeID(dirAfter))
	ExpectEq(getInodeID(barBefore), getInodeID(barAfter))

	ExpectThat(fooAfter.ModTime(), timeutil.TimeEq(newMtime))
	ExpectThat(dirAfter.ModTime(), timeutil.TimeEq(newMtime))
	ExpectThat(barAfter.ModTime(), timeutil.TimeEq(newMtime))
}

////////////////////////////////////////////////////////////////////////
// Page cache
////////////////////////////////////////////////////////////////////////

type PageCacheTest struct {
	cachingFSTest
}

var _ SetUpInterface = &PageCacheTest{}

func init() { RegisterTestSuite(&PageCacheTest{}) }

func (t *PageCacheTest) SetUp(ti *TestInfo) {
	const (
		lookupEntryTimeout = 0
		getattrTimeout     = 0
	)

	t.cachingFSTest.setUp(ti, lookupEntryTimeout, getattrTimeout)
}

func (t *PageCacheTest) SingleFileHandle_NoKeepCache() {
	t.fs.SetKeepCache(false)

	// Open the file.
	f, err := os.Open(path.Join(t.Dir, "foo"))
	AssertEq(nil, err)

	defer f.Close()

	// Read its contents once.
	f.Seek(0, 0)
	AssertEq(nil, err)

	c1, err := ioutil.ReadAll(f)
	AssertEq(nil, err)
	AssertEq(cachingfs.FooSize, len(c1))

	// And again.
	f.Seek(0, 0)
	AssertEq(nil, err)

	c2, err := ioutil.ReadAll(f)
	AssertEq(nil, err)
	AssertEq(cachingfs.FooSize, len(c2))

	// We should have seen the same contents each time.
	ExpectTrue(bytes.Equal(c1, c2))
}

func (t *PageCacheTest) SingleFileHandle_KeepCache() {
	t.fs.SetKeepCache(true)

	// Open the file.
	f, err := os.Open(path.Join(t.Dir, "foo"))
	AssertEq(nil, err)

	defer f.Close()

	// Read its contents once.
	f.Seek(0, 0)
	AssertEq(nil, err)

	c1, err := ioutil.ReadAll(f)
	AssertEq(nil, err)
	AssertEq(cachingfs.FooSize, len(c1))

	// And again.
	f.Seek(0, 0)
	AssertEq(nil, err)

	c2, err := ioutil.ReadAll(f)
	AssertEq(nil, err)
	AssertEq(cachingfs.FooSize, len(c2))

	// We should have seen the same contents each time.
	ExpectTrue(bytes.Equal(c1, c2))
}

func (t *PageCacheTest) TwoFileHandles_NoKeepCache() {
	t.fs.SetKeepCache(false)

	// SetKeepCache(false) doesn't work on OS X. See the notes on
	// OpenFileOp.KeepPageCache.
	if runtime.GOOS == "darwin" {
		return
	}

	// Open the file.
	f1, err := os.Open(path.Join(t.Dir, "foo"))
	AssertEq(nil, err)

	defer f1.Close()

	// Read its contents once.
	f1.Seek(0, 0)
	AssertEq(nil, err)

	c1, err := ioutil.ReadAll(f1)
	AssertEq(nil, err)
	AssertEq(cachingfs.FooSize, len(c1))

	// Open a second handle.
	f2, err := os.Open(path.Join(t.Dir, "foo"))
	AssertEq(nil, err)

	defer f2.Close()

	// We should see different contents if we read from that handle, due to the
	// cache being invalidated at the time of opening.
	f2.Seek(0, 0)
	AssertEq(nil, err)

	c2, err := ioutil.ReadAll(f2)
	AssertEq(nil, err)
	AssertEq(cachingfs.FooSize, len(c2))

	ExpectFalse(bytes.Equal(c1, c2))

	// Another read from the second handle should give the same result as the
	// first one from that handle.
	f2.Seek(0, 0)
	AssertEq(nil, err)

	c3, err := ioutil.ReadAll(f2)
	AssertEq(nil, err)
	AssertEq(cachingfs.FooSize, len(c3))

	ExpectTrue(bytes.Equal(c2, c3))

	// And another read from the first handle should give the same result yet
	// again.
	f1.Seek(0, 0)
	AssertEq(nil, err)

	c4, err := ioutil.ReadAll(f1)
	AssertEq(nil, err)
	AssertEq(cachingfs.FooSize, len(c4))

	ExpectTrue(bytes.Equal(c2, c4))
}

func (t *PageCacheTest) TwoFileHandles_KeepCache() {
	t.fs.SetKeepCache(true)

	// Open the file.
	f1, err := os.Open(path.Join(t.Dir, "foo"))
	AssertEq(nil, err)

	defer f1.Close()

	// Read its contents once.
	f1.Seek(0, 0)
	AssertEq(nil, err)

	c1, err := ioutil.ReadAll(f1)
	AssertEq(nil, err)
	AssertEq(cachingfs.FooSize, len(c1))

	// Open a second handle.
	f2, err := os.Open(path.Join(t.Dir, "foo"))
	AssertEq(nil, err)

	defer f2.Close()

	// We should see the same contents when we read via the second handle.
	f2.Seek(0, 0)
	AssertEq(nil, err)

	c2, err := ioutil.ReadAll(f2)
	AssertEq(nil, err)
	AssertEq(cachingfs.FooSize, len(c2))

	ExpectTrue(bytes.Equal(c1, c2))

	// Ditto if we read again from the first.
	f1.Seek(0, 0)
	AssertEq(nil, err)

	c3, err := ioutil.ReadAll(f1)
	AssertEq(nil, err)
	AssertEq(cachingfs.FooSize, len(c3))

	ExpectTrue(bytes.Equal(c1, c3))
}