This repository was archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathMockReadSeekCloserWithPseudoRandomContent_test.go
78 lines (70 loc) · 2.28 KB
/
MockReadSeekCloserWithPseudoRandomContent_test.go
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package main
import (
"io"
"math/rand"
"time"
)
// This mock reader produces virtual 5G file with programmatically-generated pseudo-random content
// where each byte is a deterministic function of its offset, so it is easy to verify
// whether reading of a chunk returns correct byte sequence
type MockReadSeekCloserWithPseudoRandomContent struct {
Rand *rand.Rand
FileSize int64
position int64
IsClosed bool
ReaderStats *ReaderStats
}
// Seek to a given position
func (this *MockReadSeekCloserWithPseudoRandomContent) Seek(pos int64) error {
this.position = pos
this.ReaderStats.IncrementSeek()
return nil
}
// Returns current posistion
func (this *MockReadSeekCloserWithPseudoRandomContent) Position() (int64, error) {
return this.position, nil
}
// Reads chunk into the specified buffer
func (this *MockReadSeekCloserWithPseudoRandomContent) Read(buf []byte) (int, error) {
// Sleeping for 1ms to yield to other threads
time.Sleep(1 * time.Millisecond)
this.ReaderStats.IncrementRead()
if this.position >= this.FileSize {
return 0, io.EOF
}
if len(buf) == 0 {
return 0, nil
}
// Deciding how many bytes to return
var nr int
if this.Rand == nil {
// If randomized isn't provided then returning as many as requested
nr = len(buf)
} else {
// Otherwise random length:
// Adding 1 to random number sicne we don't want to return 0 bytes
nr = this.Rand.Intn(len(buf)) + 1
}
// Adjusting for the case of the reading close to the end of the file
if int64(nr) > this.FileSize-this.position {
nr = int(this.FileSize - this.position)
}
// Programmatically generating data
for i := 0; i < nr; i++ {
buf[i] = generateByteAtOffset(this.position + int64(i))
}
this.position += int64(nr)
return nr, nil
}
// Closes all underlying network connections
func (this *MockReadSeekCloserWithPseudoRandomContent) Close() error {
this.IsClosed = true
return nil
}
// Getting last 8 bits of a sum of remainders of a division to various prime numbers
// this gives us pseudo-random file content which is good enough for testing scenarios
func generateByteAtOffset(o int64) byte {
return byte(o%7 + o%11 + o%13 + o%127 + o%251 + o%31337 + o%1299709)
}