Ensure that on Windows, mmap never expands the file speculatively via InitialMmapSize, and that the database remains fully usable after such an open. https://github.com/etcd-io/bbolt/issues/928
(t *testing.T)
| 1658 | // InitialMmapSize, and that the database remains fully usable after such an |
| 1659 | // open. https://github.com/etcd-io/bbolt/issues/928 |
| 1660 | func TestDB_MaxSizeExceededDoesNotGrow(t *testing.T) { |
| 1661 | if runtime.GOOS != "windows" { |
| 1662 | t.SkipNow() |
| 1663 | } |
| 1664 | |
| 1665 | // Build a filled database to use as the base for all sub-tests. |
| 1666 | // 4 MiB allocation jumps, 2000 × 1 KB entries → file lands at ~4 MiB. |
| 1667 | base := createFilledDB(t, nil, 4*1024*1024, 2000) |
| 1668 | basePath := base.Path() |
| 1669 | require.NoError(t, base.Close()) |
| 1670 | |
| 1671 | baseSize := fileSize(basePath) |
| 1672 | require.Greater(t, baseSize, int64(1024*1024), "base DB too small for test: %d", baseSize) |
| 1673 | |
| 1674 | // copyDB snapshots the base file into a fresh temp path so each sub-test |
| 1675 | // gets an independent copy. |
| 1676 | copyDB := func(t *testing.T) string { |
| 1677 | t.Helper() |
| 1678 | data, err := os.ReadFile(basePath) |
| 1679 | require.NoError(t, err) |
| 1680 | dst := filepath.Join(t.TempDir(), "db") |
| 1681 | require.NoError(t, os.WriteFile(dst, data, 0600)) |
| 1682 | return dst |
| 1683 | } |
| 1684 | |
| 1685 | testCases := []struct { |
| 1686 | name string |
| 1687 | initialMmapSize int // 0 means unset |
| 1688 | maxSize int // 0 means unset |
| 1689 | // expected file size after re-open (0 means "same as baseSize") |
| 1690 | wantSize int64 |
| 1691 | wantOpenErr error |
| 1692 | }{ |
| 1693 | { |
| 1694 | // On Windows, mmap expands the file to the mapped size immediately. |
| 1695 | // When InitialMmapSize > MaxSize, the mmap call itself would grow the |
| 1696 | // file past MaxSize, so Open is rejected with ErrMaxSizeReached rather |
| 1697 | // than silently exceeding the limit. |
| 1698 | name: "InitialMmapSize larger than file does not grow", |
| 1699 | initialMmapSize: int(baseSize) * 2, |
| 1700 | maxSize: 1, |
| 1701 | wantSize: baseSize, |
| 1702 | wantOpenErr: berrors.ErrMaxSizeReached, |
| 1703 | }, |
| 1704 | { |
| 1705 | name: "InitialMmapSize equal to file size does not grow", |
| 1706 | initialMmapSize: int(baseSize), |
| 1707 | maxSize: 1, |
| 1708 | wantSize: baseSize, |
| 1709 | }, |
| 1710 | { |
| 1711 | name: "InitialMmapSize smaller than file size does not shrink", |
| 1712 | initialMmapSize: int(baseSize) / 2, |
| 1713 | maxSize: 1, |
| 1714 | wantSize: baseSize, |
| 1715 | }, |
| 1716 | { |
| 1717 | name: "no InitialMmapSize does not grow", |
nothing calls this directly
no test coverage detected