Best Is code snippet using is.NoErr
comm.go
Source:comm.go
...47 }48 // Connection is closed49 if s == 0 {50 log.Println("Connection closed")51 return TERMMSG, syserror.NoErr52 }53 // Read returns an error (we test this only as a second test since s=0 means socket closed,54 // which returns also EOF as an error but we want to handle it as normal termination, not an error case55 if err != nil {56 log.Println("ERROR:", err.Error())57 return INVALID, syserror.ErrFatal58 }59 if s > 0 && s != 4 {60 log.Println("ERROR Cannot recv header")61 return INVALID, syserror.NoErr62 }63 // Disconnect request64 if string(hdr[:s]) == TERMMSG {65 log.Println("Recv'd disconnect request")66 return TERMMSG, syserror.NoErr67 }68 if string(hdr[:s]) == CONNREQ {69 log.Println("Recv'd connection request")70 return CONNREQ, syserror.NoErr71 }72 if string(hdr[:s]) == CONNACK {73 log.Println("Recv'd connection ACK")74 return CONNACK, syserror.NoErr75 }76 log.Println("Invalid msg header")77 return INVALID, syserror.ErrFatal78}79func (p *PeerInfo) getPayloadSize() (uint64, syserror.SysError) {80 if p == nil || p.conn == nil {81 return 0, syserror.ErrNotAvailable82 }83 ps := make([]byte, 8) // Payload size is always 8 bytes84 s, myerr := p.conn.Read(ps)85 if s != 8 || myerr != nil {86 log.Println("ERROR: expecting 8 bytes but received", s)87 return 0, syserror.ErrFatal88 }89 return binary.LittleEndian.Uint64(ps), syserror.NoErr90}91func (p *PeerInfo) getPayload(size uint64) ([]byte, syserror.SysError) {92 if p == nil || p.conn == nil {93 return nil, syserror.ErrNotAvailable94 }95 payload := make([]byte, size)96 s, myerr := p.conn.Read(payload)97 if uint64(s) != size || myerr != nil {98 log.Println("ERROR: expecting ", size, "but received", s)99 return nil, syserror.ErrFatal100 }101 return payload, syserror.NoErr102}103func (p *PeerInfo) handleConnReq(size uint64, payload []byte) syserror.SysError {104 if p == nil {105 log.Println("ERROR: local server is not initialized")106 return syserror.ErrFatal107 }108 // Send CONNACK with the payload109 log.Println("Sending CONNACK...")110 err := p.SendMsg(CONNACK, payload)111 if err != syserror.NoErr {112 return err113 }114 return syserror.NoErr115}116// HandleHandshake receives and handles a CONNREQ message, i.e., a client trying to connect117func (p *PeerInfo) HandleHandshake() syserror.SysError {118 /* Handle the CONNREQ message */119 msgtype, payload_size, payload, err := p.RecvMsg()120 if err != syserror.NoErr {121 return syserror.ErrFatal122 }123 if msgtype == CONNREQ {124 err := p.handleConnReq(payload_size, payload)125 if err != syserror.NoErr {126 return err127 }128 } else {129 return syserror.NoErr // We did not get the expected CONNREQ msg130 }131 return syserror.NoErr132}133func (p *PeerInfo) sendMsgType(msgType string) syserror.SysError {134 if p.conn == nil {135 return syserror.ErrFatal136 }137 s, err := p.conn.Write([]byte(msgType))138 if s == 0 || err != nil {139 return syserror.ErrFatal140 }141 return syserror.NoErr142}143// SendMsg sends a basic message144func (p *PeerInfo) SendMsg(msgType string, payload []byte) syserror.SysError {145 if p.conn == nil {146 return syserror.ErrFatal147 }148 hdrerr := p.sendMsgType(msgType)149 if hdrerr != syserror.NoErr {150 return hdrerr151 }152 if payload != nil {153 syserr := p.sendUint64(uint64(len(payload)))154 if syserr != syserror.NoErr {155 return syserr156 }157 s, err := p.conn.Write(payload)158 if s != len(payload) || err != nil {159 log.Println("[ERROR] write operation failed")160 return syserror.ErrFatal161 }162 } else {163 syserr := p.sendUint64(uint64(0))164 if syserr != syserror.NoErr {165 return syserr166 }167 }168 return syserror.NoErr169}170func (p *PeerInfo) sendUint64(value uint64) syserror.SysError {171 if p == nil || p.conn == nil {172 return syserror.ErrFatal173 }174 buff := make([]byte, 8)175 if buff == nil {176 return syserror.ErrOutOfRes177 }178 binary.LittleEndian.PutUint64(buff, value)179 s, myerr := p.conn.Write(buff)180 if myerr != nil {181 log.Println(myerr.Error())182 }183 if myerr == nil && s != 8 {184 log.Println("Received ", s, "bytes, instead of 8")185 }186 if s != 8 || myerr != nil {187 return syserror.ErrFatal188 }189 return syserror.NoErr190}191// RecvMsg receives a basic message192func (p *PeerInfo) RecvMsg() (string, uint64, []byte, syserror.SysError) {193 if p == nil || p.conn == nil {194 return "", 0, nil, syserror.ErrFatal195 }196 msgtype, err := p.GetHeader()197 // Messages without payload198 if msgtype == "TERM" || msgtype == "INVA" || err != syserror.NoErr {199 return msgtype, 0, nil, syserror.ErrFatal200 }201 // Get the payload size202 payload_size, err := p.getPayloadSize()203 if err != syserror.NoErr {204 return msgtype, 0, nil, syserror.ErrFatal205 }206 if payload_size == 0 {207 return msgtype, 0, nil, syserror.NoErr208 }209 // Get the payload210 buff, err := p.getPayload(payload_size)211 if err != syserror.NoErr {212 return msgtype, payload_size, buff, syserror.NoErr213 }214 return msgtype, payload_size, buff, syserror.NoErr215}216// ConnectHandshake initiates a connection handshake217func (p *PeerInfo) ConnectHandshake() syserror.SysError {218 err := p.SendMsg(CONNREQ, nil)219 if err != syserror.NoErr {220 return err221 }222 // Receive the CONNACK, the payload is the block_sizw223 msgtype, size, _, recverr := p.RecvMsg()224 if recverr != syserror.NoErr || msgtype != CONNACK || size < 0 {225 return syserror.ErrFatal226 }227 // todo: handle payload228 return syserror.NoErr229}230func (p *PeerInfo) connectHandshake() syserror.SysError {231 err := p.SendMsg(CONNREQ, nil)232 if err != syserror.NoErr {233 return err234 }235 // Receive the CONNACK, the payload is the block_sizw236 msgtype, s, _, err := p.RecvMsg()237 if err != syserror.NoErr || msgtype != CONNACK || s < 0 {238 return syserror.ErrFatal239 }240 return syserror.NoErr241}242func (p *PeerInfo) Connect() syserror.SysError {243 retry := 0244 if p == nil {245 return syserror.ErrFatal246 }247 var err error248Retry:249 p.conn, err = net.Dial("tcp", p.URL)250 if err != nil {251 log.Printf("Dial failed: %s", err.Error())252 if retry < 5 {253 retry++254 log.Printf("Retrying after %d seconds\n", retry)255 time.Sleep(time.Duration(retry) * time.Second)256 goto Retry257 }258 return syserror.ErrOutOfRes259 }260 syserr := p.connectHandshake()261 if syserr != syserror.NoErr {262 return syserror.ErrFatal263 }264 return syserror.NoErr265}266func (info *PeerInfo) doServer() syserror.SysError {267 done := 0268 syserr := info.HandleHandshake()269 if syserr != syserror.NoErr {270 log.Printf("[ERROR] handshake with client failed: %s", syserr.Error())271 }272 for done != 1 {273 // Handle the message header274 msgtype, syserr := info.GetHeader()275 if syserr != syserror.NoErr {276 done = 1277 }278 if msgtype == "INVA" || msgtype == "TERM" {279 done = 1280 }281 // Handle the paylaod size282 payload_size, pserr := info.getPayloadSize()283 if pserr != syserror.NoErr {284 done = 1285 }286 // Handle the payload when necessary287 var payload []byte = nil288 if payload_size != 0 {289 payload, syserr = info.getPayload(payload_size)290 }291 if payload_size != 0 && payload != nil {292 return syserror.ErrFatal293 }294 }295 info.conn.Close()296 log.Printf("Go routine for peer %s done\n", info.URL)297 return syserror.NoErr298}299func (info *PeerInfo) CreateEmbeddedServer() syserror.SysError {300 if info == nil {301 return syserror.ErrFatal302 }303 log.Println("Creating embedded server...")304 listener, err := net.Listen("tcp", info.URL)305 if err != nil {306 return syserror.ErrFatal307 }308 log.Println("Server created on", info.URL)309 var conn net.Conn310 for {311 conn, err = listener.Accept()312 log.Println("Connection accepted")313 var newPeer PeerInfo314 newPeer.conn = conn315 if err != nil {316 log.Println("[ERROR] ", err.Error())317 }318 log.Println("Creating new Go routine for new peer...")319 go newPeer.doServer()320 }321 // todo: fix termination322 //return syserror.NoErr323}324func (info *PeerInfo) CreateServer() (PeerInfo, syserror.SysError) {325 var newPeer PeerInfo326 if info == nil {327 return newPeer, syserror.ErrFatal328 }329 listener, err := net.Listen("tcp", info.URL)330 if err != nil {331 log.Printf("failed to listen on socket: %s", err)332 return newPeer, syserror.ErrFatal333 }334 if info.timeout > 0 {335 listener.(*net.TCPListener).SetDeadline(time.Now().Add(time.Duration(info.timeout) * time.Second))336 }337 newPeer.conn, err = listener.Accept()338 if err != nil {339 return newPeer, syserror.ErrFatal340 }341 return newPeer, syserror.NoErr342}...
client_xapi_object_test.go
Source:client_xapi_object_test.go
...11 _ = createTestBucket(testPutBucketName)12 const path = "TestClientXAPIObject/test"13 // check non-existed object14 _, err := svc.XDeleteObjectFromPath(ctx, testPutBucketName, path)15 is.NoErr(err)16 ok, err := svc.XExistObject(ctx, testPutBucketName, path)17 is.NoErr(err)18 is.Equal(ok, false)19 resGet, err := svc.XGetObjectFromPath(ctx, testPutBucketName, path)20 is.NoErr(err)21 is.Equal(resGet.Exists, false)22 // check existed object23 resPut, err := svc.XPutObjectToPath(ctx, testPutBucketName, path, []byte("001"))24 is.NoErr(err)25 is.True(resPut.ETag != "") // ETag must be set26 ok, err = svc.XExistObject(ctx, testPutBucketName, path)27 is.NoErr(err)28 is.True(ok)29 resGet, err = svc.XGetObjectFromPath(ctx, testPutBucketName, path)30 is.NoErr(err)31 is.Equal(resGet.ETag, resPut.ETag) // ETag must be same32 byt, err := resGet.ToBytes()33 is.NoErr(err)34 is.Equal(byt, []byte("001")) // content must be updated35 // update existed object36 resPut, err = svc.XPutObjectToPath(ctx, testPutBucketName, path, []byte("002"))37 is.NoErr(err)38 is.True(resPut.ETag != "") // ETag must be set39 resGet, err = svc.XGetObjectFromPath(ctx, testPutBucketName, path)40 is.NoErr(err)41 is.Equal(resGet.ETag, resPut.ETag) // ETag must be same42 byt, err = resGet.ToBytes()43 is.NoErr(err)44 is.Equal(byt, []byte("002")) // content must be updated45 // check deleted object46 _, err = svc.XDeleteObjectFromPath(ctx, testPutBucketName, path)47 is.NoErr(err)48 ok, err = svc.XExistObject(ctx, testPutBucketName, path)49 is.NoErr(err)50 is.Equal(ok, false)51 resGet, err = svc.XGetObjectFromPath(ctx, testPutBucketName, path)52 is.NoErr(err)53 is.Equal(resGet.Exists, false)54 _, err = svc.XDeleteObjectFromPath(ctx, testPutBucketName, path)55 is.NoErr(err)56}...
rw_test.go
Source:rw_test.go
...12 bits = 3213 rate = 4410014 )15 f, err := ioutil.TempFile("", "wavPkgtest")16 is.NoErr(err)17 testFname := f.Name()18 meta := File{19 Channels: 1,20 SampleRate: rate,21 SignificantBits: bits,22 }23 writer, err := meta.NewWriter(f)24 is.NoErr(err)25 var freq float6426 freq = 0.000127 // one second28 for n := 0; n < rate; n++ {29 y := int32(0.8 * math.Pow(2, bits-1) * math.Sin(freq*float64(n)))30 freq += 0.00000231 err = writer.WriteInt32(y)32 if err != nil {33 t.Fatal(err)34 }35 }36 err = writer.Close()37 is.NoErr(err)38 f, err = os.Open(testFname)39 is.NoErr(err)40 stat, err := f.Stat()41 is.NoErr(err)42 _, err = NewReader(f, stat.Size())43 is.NoErr(err)44 is.NoErr(os.Remove(testFname))45}46func TestWriteRead_Sample(t *testing.T) {47 is := is.New(t)48 const (49 bits = 1650 rate = 4410051 )52 f, err := ioutil.TempFile("", "wavPkgtest")53 is.NoErr(err)54 testFname := f.Name()55 meta := File{56 Channels: 1,57 SampleRate: rate,58 SignificantBits: bits,59 }60 writer, err := meta.NewWriter(f)61 is.NoErr(err)62 var freq float6463 freq = 0.000164 var s = make([]byte, 2)65 // one second66 for n := 0; n < rate; n++ {67 y := int32(0.8 * math.Pow(2, bits-1) * math.Sin(freq*float64(n)))68 freq += 0.00000269 // s[3] = byte((y >> 24) & 0xFF)70 // s[2] = byte((y >> 16) & 0xFF)71 s[1] = byte((y >> 8) & 0xFF)72 s[0] = byte(y & 0xFF)73 err = writer.WriteSample(s)74 if err != nil {75 t.Fatal(err)76 }77 }78 err = writer.Close()79 is.NoErr(err)80 f, err = os.Open(testFname)81 is.NoErr(err)82 stat, err := f.Stat()83 is.NoErr(err)84 _, err = NewReader(f, stat.Size())85 is.NoErr(err)86 is.NoErr(os.Remove(testFname))87}...
NoErr
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println("Hello, playground")4}5import "fmt"6func main() {7 fmt.Println("Hello, playground")8}9import "fmt"10func main() {11 fmt.Println("Hello, playground")12}13import "fmt"14func main() {15 fmt.Println("Hello, playground")16}17import "fmt"18func main() {19 fmt.Println("Hello, playground")20}21import "fmt"22func main() {23 fmt.Println("Hello, playground")24}25import "fmt"26func main() {27 fmt.Println("Hello, playground")28}29import "fmt"30func main() {31 fmt.Println("Hello, playground")32}33import "fmt"34func main() {35 fmt.Println("Hello, playground")36}37import "fmt"38func main() {39 fmt.Println("Hello, playground")40}41import "fmt"42func main() {43 fmt.Println("Hello, playground")44}45import "fmt"46func main() {47 fmt.Println("Hello, playground")48}49import "fmt"50func main() {51 fmt.Println("Hello, playground")52}53import "fmt"54func main() {55 fmt.Println("Hello, playground
NoErr
Using AI Code Generation
1import (2func main() {3 f, err := os.Open("notfound.txt")4 if err != nil {5 fmt.Println(err)6 }7 fmt.Println(f.Name(), "opened successfully")8}9import (10func main() {11 f, err := os.Open("notfound.txt")12 if err != nil {13 if os.IsNotExist(err) {14 fmt.Println("File does not exist")15 }16 }17 fmt.Println(f.Name(), "opened successfully")18}19import (20func main() {21 f, err := os.Open("notfound.txt")22 if err != nil {23 if os.IsPermission(err) {24 fmt.Println("Permission denied")25 }26 }27 fmt.Println(f.Name(), "opened successfully")28}29import (30func main() {31 f, err := os.Open("notfound.txt")32 if err != nil {33 if os.IsTimeout(err) {34 fmt.Println("Timeout")35 }36 }37 fmt.Println(f.Name(), "opened successfully")38}39import (40func main() {41 f, err := os.Open("notfound.txt")42 if err != nil {43 if os.IsExist(err) {44 fmt.Println("File already exists")45 }46 }47 fmt.Println(f.Name(), "opened successfully")48}49import (50func main() {51 f, err := os.Open("notfound.txt")52 if err != nil {53 if os.IsPathSeparator(err) {54 fmt.Println("Path separator")55 }56 }57 fmt.Println(f.Name(), "opened successfully")58}
NoErr
Using AI Code Generation
1import (2type Config struct {3}4func main() {5 err := envconfig.Process("app", &c)6 if err != nil {7 fmt.Println("error")8 }9 fmt.Println(c.Port)10}11import (12type Config struct {13}14func main() {15 err := envconfig.Process("app", &c)16 if err != nil {17 fmt.Println("error")18 }19 fmt.Println(c.Port)20}21import (22type Config struct {23}24func main() {25 err := envconfig.Process("app", &c)26 if err != nil {27 fmt.Println("error")28 }29 fmt.Println(c.Port)30}31import (32type Config struct {33}34func main() {35 err := envconfig.Process("app", &c)36 if err != nil {37 fmt.Println("error")38 }39 fmt.Println(c.Port)40}41import (42type Config struct {43}44func main() {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!