storage/test/unit/test_chunk_growth.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/test/unit/test_chunk_growth.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,55 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.6 + */
     1.7 +
     1.8 +// This file tests SQLITE_FCNTL_CHUNK_SIZE behaves as expected
     1.9 +
    1.10 +function run_sql(d, sql) {
    1.11 +  var stmt = d.createStatement(sql)
    1.12 +  stmt.execute()
    1.13 +  stmt.finalize();
    1.14 +}
    1.15 +
    1.16 +function new_file(name)
    1.17 +{
    1.18 +  var file = dirSvc.get("ProfD", Ci.nsIFile);
    1.19 +  file.append(name);
    1.20 +  return file;
    1.21 +}
    1.22 +
    1.23 +function get_size(name) {
    1.24 +  return new_file(name).fileSize
    1.25 +}
    1.26 +
    1.27 +function run_test()
    1.28 +{
    1.29 +  const filename = "chunked.sqlite";
    1.30 +  const CHUNK_SIZE = 512 * 1024;
    1.31 +  var d = getDatabase(new_file(filename));
    1.32 +  try {
    1.33 +    d.setGrowthIncrement(CHUNK_SIZE, "");
    1.34 +  } catch (e if e.result == Cr.NS_ERROR_FILE_TOO_BIG) {
    1.35 +    print("Too little free space to set CHUNK_SIZE!");
    1.36 +    return;
    1.37 +  }
    1.38 +  run_sql(d, "CREATE TABLE bloat(data varchar)");
    1.39 +
    1.40 +  var orig_size = get_size(filename);
    1.41 +  /* Dump in at least 32K worth of data.
    1.42 +   * While writing ensure that the file size growth in chunksize set above.
    1.43 +   */
    1.44 +  const str1024 = new Array(1024).join("T");
    1.45 +  for(var i = 0; i < 32; i++) {
    1.46 +    run_sql(d, "INSERT INTO bloat VALUES('" + str1024 + "')");
    1.47 +    var size = get_size(filename)
    1.48 +    // Must not grow in small increments.
    1.49 +    do_check_true(size == orig_size || size >= CHUNK_SIZE);
    1.50 +  }
    1.51 +  /* In addition to growing in chunk-size increments, the db
    1.52 +   * should shrink in chunk-size increments too.
    1.53 +   */
    1.54 +  run_sql(d, "DELETE FROM bloat")
    1.55 +  run_sql(d, "VACUUM")
    1.56 +  do_check_true(get_size(filename) >= CHUNK_SIZE)
    1.57 +}
    1.58 +

mercurial