gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/miningpool/dependencies.go (about) 1 package pool 2 3 import ( 4 "errors" 5 "io/ioutil" 6 "net" 7 "os" 8 9 "gitlab.com/NebulousLabs/fastrand" 10 "gitlab.com/SiaPrime/SiaPrime/persist" 11 ) 12 13 // Fake errors that get returned when a simulated failure of a dependency is 14 // desired for testing. 15 var ( 16 mockErrListen = errors.New("simulated Listen failure") 17 mockErrLoadFile = errors.New("simulated LoadFile failure") 18 mockErrMkdirAll = errors.New("simulated MkdirAll failure") 19 mockErrNewLogger = errors.New("simulated NewLogger failure") 20 mockErrOpenDatabase = errors.New("simulated OpenDatabase failure") 21 mockErrReadFile = errors.New("simulated ReadFile failure") 22 mockErrRemoveFile = errors.New("simulated RemoveFile faulure") 23 mockErrSymlink = errors.New("simulated Symlink failure") 24 mockErrWriteFile = errors.New("simulated WriteFile failure") 25 ) 26 27 // These interfaces define the Pool's dependencies. Mocking implementation 28 // complexity can be reduced by defining each dependency as the minimum 29 // possible subset of the real dependency. 30 type ( 31 // dependencies defines all of the dependencies of the Pool. 32 dependencies interface { 33 // disrupt can be inserted in the code as a way to inject problems, 34 // such as a network call that take 10 minutes or a disk write that 35 // never completes. disrupt will return true if the disruption is 36 // forcibly triggered. In production, disrupt will always return false. 37 disrupt(string) bool 38 39 // listen gives the host the ability to receive incoming connections. 40 listen(string, string) (net.Listener, error) 41 42 // loadFile allows the host to load a persistence structure form disk. 43 loadFile(persist.Metadata, interface{}, string) error 44 45 // mkdirAll gives the host the ability to create chains of folders 46 // within the filesystem. 47 mkdirAll(string, os.FileMode) error 48 49 // newLogger creates a logger that the host can use to log messages and 50 // write critical statements. 51 newLogger(string) (*persist.Logger, error) 52 53 // openDatabase creates a database that the host can use to interact 54 // with large volumes of persistent data. 55 openDatabase(persist.Metadata, string) (*persist.BoltDatabase, error) 56 57 // randRead fills the input bytes with random data. 58 randRead([]byte) (int, error) 59 60 // readFile reads a file in full from the filesystem. 61 readFile(string) ([]byte, error) 62 63 // removeFile removes a file from file filesystem. 64 removeFile(string) error 65 66 // symlink creates a sym link between a source and a destination. 67 symlink(s1, s2 string) error 68 69 // writeFile writes data to the filesystem using the provided filename. 70 writeFile(string, []byte, os.FileMode) error 71 } 72 ) 73 74 type ( 75 // productionDependencies is an empty struct that implements all of the 76 // dependencies using full featured libraries. 77 productionDependencies struct{} 78 ) 79 80 // disrupt will always return false, but can be over-written during testing to 81 // trigger disruptions. 82 func (productionDependencies) disrupt(string) bool { 83 return false 84 } 85 86 // listen gives the host the ability to receive incoming connections. 87 func (productionDependencies) listen(s1, s2 string) (net.Listener, error) { 88 return net.Listen(s1, s2) 89 } 90 91 // loadFile allows the host to load a persistence structure form disk. 92 func (productionDependencies) loadFile(m persist.Metadata, i interface{}, s string) error { 93 return persist.LoadJSON(m, i, s) 94 } 95 96 // mkdirAll gives the host the ability to create chains of folders within the 97 // filesystem. 98 func (productionDependencies) mkdirAll(s string, fm os.FileMode) error { 99 return os.MkdirAll(s, fm) 100 } 101 102 // newLogger creates a logger that the host can use to log messages and write 103 // critical statements. 104 func (productionDependencies) newLogger(s string) (*persist.Logger, error) { 105 return persist.NewFileLogger(s) 106 } 107 108 // openDatabase creates a database that the host can use to interact with large 109 // volumes of persistent data. 110 func (productionDependencies) openDatabase(m persist.Metadata, s string) (*persist.BoltDatabase, error) { 111 return persist.OpenDatabase(m, s) 112 } 113 114 // randRead fills the input bytes with random data. 115 func (productionDependencies) randRead(b []byte) (int, error) { 116 return fastrand.Reader.Read(b) 117 } 118 119 // readFile reads a file from the filesystem. 120 func (productionDependencies) readFile(s string) ([]byte, error) { 121 return ioutil.ReadFile(s) 122 } 123 124 // removeFile removes a file from the filesystem. 125 func (productionDependencies) removeFile(s string) error { 126 return os.Remove(s) 127 } 128 129 // symlink creates a symlink between a source and a destination file. 130 func (productionDependencies) symlink(s1, s2 string) error { 131 return os.Symlink(s1, s2) 132 } 133 134 // writeFile writes a file to the filesystem. 135 func (productionDependencies) writeFile(s string, b []byte, fm os.FileMode) error { 136 return ioutil.WriteFile(s, b, fm) 137 }