TestLogRotate writes enough log file entries to cause 1 file rotation.
(t *testing.T)
| 54 | |
| 55 | // TestLogRotate writes enough log file entries to cause 1 file rotation. |
| 56 | func TestLogRotate(t *testing.T) { |
| 57 | dir := t.TempDir() |
| 58 | el, err := openWal(dir) |
| 59 | fmt.Println(dir) |
| 60 | require.NoError(t, err) |
| 61 | |
| 62 | // Generate deterministic entries using a seed. |
| 63 | const SEED = 1 |
| 64 | rnd := rand.New(rand.NewSource(SEED)) |
| 65 | |
| 66 | makeEntry := func(i int) raftpb.Entry { |
| 67 | // Be careful when changing this value, as it could easily end up filling up |
| 68 | // the entire tmpfs. Currently, this writes ~1.5GB. |
| 69 | data := make([]byte, rnd.Intn(1<<16)) |
| 70 | rnd.Read(data) |
| 71 | return raftpb.Entry{Index: uint64(i + 1), Term: 1, Data: data} |
| 72 | } |
| 73 | |
| 74 | // Write enough entries to fill ~1.5x logfiles, causing a rotation. |
| 75 | const totalEntries = (maxNumEntries * 3) / 2 |
| 76 | totalBytes := 0 |
| 77 | for i := range totalEntries { |
| 78 | entry := makeEntry(i) |
| 79 | require.NoError(t, el.AddEntries([]raftpb.Entry{entry})) |
| 80 | totalBytes += len(entry.Data) |
| 81 | } |
| 82 | log.Printf("Wrote %d bytes", totalBytes) |
| 83 | |
| 84 | // Reopen the file and retrieve all entries. |
| 85 | el, err = openWal(dir) |
| 86 | require.NoError(t, err) |
| 87 | entries := el.allEntries(0, math.MaxInt64, math.MaxInt64) |
| 88 | require.Equal(t, totalEntries, len(entries)) |
| 89 | |
| 90 | // Use the previous seed to verify the written entries. |
| 91 | rnd = rand.New(rand.NewSource(SEED)) |
| 92 | for i, gotEntry := range entries { |
| 93 | expEntry := makeEntry(i) |
| 94 | require.Equal(t, len(expEntry.Data), len(gotEntry.Data)) |
| 95 | if len(expEntry.Data) > 0 { |
| 96 | require.Equal(t, expEntry.Data, gotEntry.Data) |
| 97 | } |
| 98 | require.Equal(t, expEntry.Index, gotEntry.Index) |
| 99 | require.Equal(t, expEntry.Term, gotEntry.Term) |
| 100 | require.Equal(t, expEntry.Type, gotEntry.Type) |
| 101 | } |
| 102 | |
| 103 | // 1 filled logfile should be present in files, |
| 104 | // and 1 partially filled logfile should be present in current. |
| 105 | require.Len(t, el.files, 1) |
| 106 | require.NotNil(t, el.current) |
| 107 | } |
| 108 | |
| 109 | // TestLogGrow writes data of sufficient size to grow the log file. |
| 110 | func TestLogGrow(t *testing.T) { |
nothing calls this directly
no test coverage detected