Ensure that a database cannot exceed its maximum size https://github.com/etcd-io/bbolt/issues/928
(t *testing.T)
| 1544 | // Ensure that a database cannot exceed its maximum size |
| 1545 | // https://github.com/etcd-io/bbolt/issues/928 |
| 1546 | func TestDB_MaxSizeNotExceeded(t *testing.T) { |
| 1547 | testCases := []struct { |
| 1548 | name string |
| 1549 | options bolt.Options |
| 1550 | }{ |
| 1551 | { |
| 1552 | name: "Standard case", |
| 1553 | options: bolt.Options{ |
| 1554 | MaxSize: 5 * 1024 * 1024, // 5 MiB |
| 1555 | PageSize: 4096, |
| 1556 | }, |
| 1557 | }, |
| 1558 | { |
| 1559 | name: "NoGrowSync", |
| 1560 | options: bolt.Options{ |
| 1561 | MaxSize: 5 * 1024 * 1024, // 5 MiB |
| 1562 | PageSize: 4096, |
| 1563 | NoGrowSync: true, |
| 1564 | }, |
| 1565 | }, |
| 1566 | } |
| 1567 | |
| 1568 | for _, testCase := range testCases { |
| 1569 | t.Run(testCase.name, func(t *testing.T) { |
| 1570 | db := createFilledDB(t, |
| 1571 | &testCase.options, |
| 1572 | 4*1024*1024, // adjust allocation jumps to 4 MiB |
| 1573 | 2000, |
| 1574 | ) |
| 1575 | |
| 1576 | path := db.Path() |
| 1577 | |
| 1578 | // The data file should be 4 MiB now (expanded once from zero). |
| 1579 | // It should have space for roughly one more entry with value size 100kB before trying to grow |
| 1580 | // This next insert should be too big |
| 1581 | err := fillDBWithKeys(db, 2, 100000) |
| 1582 | assert.ErrorIs(t, err, berrors.ErrMaxSizeReached) |
| 1583 | |
| 1584 | newSz := fileSize(path) |
| 1585 | require.Greater(t, newSz, int64(0), "unexpected new file size: %d", newSz) |
| 1586 | assert.LessOrEqual(t, newSz, int64(db.MaxSize), "The size of the data file should not exceed db.MaxSize") |
| 1587 | |
| 1588 | // Now try another write that shouldn't increase the max size |
| 1589 | err = fillDBWithKeys(db, 1, 1) |
| 1590 | assert.NoError(t, err, "Adding an entry after a failed, oversized write should not error") |
| 1591 | |
| 1592 | err = db.Close() |
| 1593 | require.NoError(t, err, "Closing the re-opened database should succeed") |
| 1594 | }) |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | // Ensure that opening a database that is beyond the maximum size succeeds |
| 1599 | // The maximum size should only apply to growing the data file |
nothing calls this directly
no test coverage detected