You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.5 KiB

  1. package storage
  2. import (
  3. "crypto/rand"
  4. "github.com/chrislusf/seaweedfs/weed/storage/types"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "testing"
  9. )
  10. func TestAppend(t *testing.T) {
  11. n := &Needle{
  12. Cookie: types.Cookie(123), // Cookie Cookie `comment:"random number to mitigate brute force lookups"`
  13. Id: types.NeedleId(123), // Id NeedleId `comment:"needle id"`
  14. Size: 8, // Size uint32 `comment:"sum of DataSize,Data,NameSize,Name,MimeSize,Mime"`
  15. DataSize: 4, // DataSize uint32 `comment:"Data size"` //version2
  16. Data: []byte("abcd"), // Data []byte `comment:"The actual file data"`
  17. Flags: 0, // Flags byte `comment:"boolean flags"` //version2
  18. NameSize: 0, // NameSize uint8 //version2
  19. Name: nil, // Name []byte `comment:"maximum 256 characters"` //version2
  20. MimeSize: 0, // MimeSize uint8 //version2
  21. Mime: nil, // Mime []byte `comment:"maximum 256 characters"` //version2
  22. PairsSize: 0, // PairsSize uint16 //version2
  23. Pairs: nil, // Pairs []byte `comment:"additional name value pairs, json format, maximum 6
  24. LastModified: 123, // LastModified uint64 //only store LastModifiedBytesLength bytes, which is 5 bytes
  25. Ttl: nil, // Ttl *TTL
  26. Checksum: 123, // Checksum CRC `comment:"CRC32 to check integrity"`
  27. AppendAtNs: 123, // AppendAtNs uint64 `comment:"append timestamp in nano seconds"` //version3
  28. Padding: nil, // Padding []byte `comment:"Aligned to 8 bytes"`
  29. }
  30. tempFile, err := ioutil.TempFile("", ".dat")
  31. if err != nil {
  32. t.Errorf("Fail TempFile. %v", err)
  33. return
  34. }
  35. /*
  36. uint8 : 0 to 255
  37. uint16 : 0 to 65535
  38. uint32 : 0 to 4294967295
  39. uint64 : 0 to 18446744073709551615
  40. int8 : -128 to 127
  41. int16 : -32768 to 32767
  42. int32 : -2147483648 to 2147483647
  43. int64 : -9223372036854775808 to 9223372036854775807
  44. */
  45. fileSize := int64(4294967295) + 10000
  46. io.CopyN(tempFile, rand.Reader, fileSize)
  47. defer func() {
  48. tempFile.Close()
  49. os.Remove(tempFile.Name())
  50. }()
  51. offset, _, _, _ := n.Append(tempFile, CurrentVersion)
  52. if offset != uint64(fileSize) {
  53. t.Errorf("Fail to Append Needle.")
  54. }
  55. }