# 2009 April 25
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# $Id: async4.test,v 1.4 2009/06/05 17:09:12 drh Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# Do not use a codec for tests in this file, as the database file is
# manipulated directly using tcl scripts (using the [hexio_write] command).
#
do_not_use_codec

# These tests only work for Tcl version 8.5 and later on Windows (for now)
#
if {$tcl_platform(platform)=="windows"} {
  scan $::tcl_version %f vx
  if {$vx<8.5} {
    finish_test
    return
  }
}

if {[info commands sqlite3async_initialize] eq ""} {
  # The async logic is not built into this system
  finish_test
  return
}
db close

# Test layout:
#
#   async4.1.*: Test the lockfiles parameter.
#   async4.2.*: Test the delay parameter.

do_test async4.1.1 {
  sqlite3async_initialize {} 0
  sqlite3async_control lockfiles
} {1}
do_test async4.1.2 {
  sqlite3async_control lockfiles false
} {0}
do_test async4.1.3 {
  sqlite3async_control lockfiles
} {0}
do_test async4.1.4 {
  sqlite3async_control lockfiles true
} {1}

do_test async4.1.5 {
  sqlite3 db test.db -vfs sqlite3async
  execsql { CREATE TABLE t1(a, b, c) }
} {}
do_test async4.1.6 {
  list [file exists test.db] [file size test.db]
} {1 0}
do_test async4.1.7 {
  sqlite3 db2 test.db
  catchsql { CREATE TABLE t2(a, b, c) } db2
} {1 {database is locked}}
do_test async4.1.8 {
  sqlite3async_control halt idle
  sqlite3async_start
  sqlite3async_wait
} {}
do_test async4.1.9 {
  catchsql { CREATE TABLE t2(a, b, c) } db2
} {0 {}}
do_test async4.1.10 {
  list [catch {sqlite3async_control lockfiles false} msg] $msg
} {1 SQLITE_MISUSE}
do_test async4.1.11 {
  db close
  list [catch {sqlite3async_control lockfiles false} msg] $msg
} {1 SQLITE_MISUSE}
do_test async4.1.12 {
  sqlite3async_start
  sqlite3async_wait
  sqlite3async_control lockfiles false
} {0}
do_test async4.1.13 {
  sqlite3 db test.db -vfs sqlite3async
  execsql { CREATE TABLE t3(a, b, c) } db
} {}
do_test async4.1.14 {
  execsql { 
    CREATE INDEX i1 ON t2(a);
    CREATE INDEX i2 ON t1(a);
  } db2
} {}
do_test async4.1.15 {
  sqlite3async_start
  sqlite3async_wait
  hexio_write test.db 28 00000000
  execsql { pragma integrity_check } db2
} {{*** in database main ***
Page 5 is never used}}
do_test async4.1.16 {
  db close
  db2 close
  sqlite3async_start
  sqlite3async_wait
} {}
do_test async4.1.17 {
  sqlite3async_control lockfiles true
} {1}

do_test async4.2.1 {
  sqlite3async_control delay
} {0}
do_test async4.2.2 {
  sqlite3async_control delay 23
} {23}
do_test async4.2.3 {
  sqlite3async_control delay
} {23}
do_test async4.2.4 {
  sqlite3async_control delay 0
} {0}
do_test async4.2.5 {
  sqlite3 db test.db -vfs sqlite3async

  execsql { CREATE TABLE t4(a, b) }
  set T1 [lindex [time {
    sqlite3async_start
    sqlite3async_wait
  }] 0]

  sqlite3async_control delay 100
  execsql { CREATE TABLE t5(a, b) }
  set T2 [lindex [time {
    sqlite3async_start
    sqlite3async_wait
  }] 0]

  expr {($T1+1000000) < $T2}
} {1}

do_test async4.2.6 {
  sqlite3async_control delay 0
  execsql { CREATE TABLE t6(a, b) }
  set T1 [lindex [time {
    sqlite3async_start
    sqlite3async_wait
  }] 0]

  expr {($T1+1000000) < $T2}
} {1}

do_test async4.2.7 {
  list [catch { sqlite3async_control delay -1 } msg] $msg
} {1 SQLITE_MISUSE}

do_test async4.2.8 {
  db close
  sqlite3async_start
  sqlite3async_wait
} {}

finish_test
