github.com/apptainer/singularity@v3.1.1+incompatible/pkg/util/copy/writer_test.go (about) 1 // Copyright (c) 2019, Sylabs Inc. All rights reserved. 2 // This software is licensed under a 3-clause BSD license. Please consult the 3 // LICENSE.md file distributed with the sources of this project regarding your 4 // rights to use or distribute this software. 5 6 package copy 7 8 import ( 9 "bytes" 10 "testing" 11 12 "github.com/sylabs/singularity/internal/pkg/test" 13 ) 14 15 func TestMultiWriter(t *testing.T) { 16 test.DropPrivilege(t) 17 defer test.ResetPrivilege(t) 18 19 // Create multiwriter instance 20 mw := &MultiWriter{} 21 22 // Write some bytes, not duplicated since there is no writer 23 n, err := mw.Write([]byte("test")) 24 if err != nil { 25 t.Error(err) 26 } 27 if n != 4 { 28 t.Errorf("wrong number of bytes written") 29 } 30 31 // Create two writers 32 buf1 := new(bytes.Buffer) 33 buf2 := new(bytes.Buffer) 34 35 // no effect 36 mw.Add(nil) 37 38 // Add first writer 39 mw.Add(buf1) 40 41 // Write some bytes 42 n, err = mw.Write([]byte("test")) 43 if err != nil { 44 t.Error(err) 45 } 46 if n != 4 { 47 t.Errorf("wrong number of bytes written") 48 } 49 50 // Check if first writer get the right content 51 if buf1.String() != "test" { 52 t.Errorf("wrong data returned") 53 } 54 55 // Reset buffer content for later check 56 buf1.Reset() 57 58 // Remove it from writer queue 59 mw.Del(buf1) 60 61 // Add second writer 62 mw.Add(buf2) 63 64 n, err = mw.Write([]byte("test")) 65 if err != nil { 66 t.Error(err) 67 } 68 if n != 4 { 69 t.Errorf("wrong number of bytes written") 70 } 71 72 // Check if second writer get the right content 73 if buf2.String() != "test" { 74 t.Errorf("wrong data returned") 75 } 76 77 // Check that first writer has empty buffer 78 if buf1.String() != "" { 79 t.Errorf("unexpected data in buf1") 80 } 81 82 mw.Del(buf2) 83 }