# 2017 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.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this script is testing the server mode of SQLite.
#


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

source $testdir/server_common.tcl
return_if_no_server

db close
foreach f [glob -nocomplain test.db*] {
  forcedelete $f
}

#-------------------------------------------------------------------------
# Check that the *-journal* files are deleted correctly.
#
server_reset_db
do_execsql_test 1.0 {
  CREATE TABLE t1(a, b);
} {}

do_test 1.1 {
  lsort [glob -nocomplain test.db-journal/*]
} {test.db-journal/0-journal}

do_test 1.2 {
  db close
  lsort [glob -nocomplain test.db-journal/*]
} {}

server_sqlite3 db test.db
do_execsql_test 1.3 {
  CREATE TABLE t2(a, b);
} {}

server_sqlite3 db2 test.db
do_test 1.4 {
  db eval {
    BEGIN;
      INSERT INTO t1 VALUES(1, 2);
  }
  db2 eval {
    BEGIN;
      INSERT INTO t2 VALUES(3, 4);
  }
} {}

do_test 1.5 {
  db2 eval COMMIT
  db eval COMMIT
  lsort [glob -nocomplain test.db-journal/*]
} {test.db-journal/0-journal test.db-journal/1-journal}

do_test 1.6 {
  db close
  lsort [glob -nocomplain test.db-journal/*]
} {test.db-journal/0-journal test.db-journal/1-journal}

do_test 1.7 {
  db2 close
  lsort [glob -nocomplain test.db-journal/*]
} {}

#-------------------------------------------------------------------------
#
server_reset_db
server_sqlite3 db2 test.db

do_execsql_test 2.0 {
  CREATE TABLE t1(a, b);
  CREATE TABLE t2(c, d);
}

# Two concurrent transactions committed.
#
do_test 2.1 {
  db eval {
    BEGIN;
      INSERT INTO t1 VALUES(1, 2);
  }
  db2 eval {
    BEGIN;
      INSERT INTO t2 VALUES(3, 4);
  }
} {}
do_test 2.2 {
  lsort [glob -nocomplain test.db-journal/*]
} {test.db-journal/0-journal test.db-journal/1-journal}
do_test 2.3.1 { db eval COMMIT  } {}
do_test 2.3.2 { db2 eval COMMIT } {}
do_execsql_test 2.4 {SELECT * FROM t1, t2} {1 2 3 4}
do_test 2.5 {
  lsort [glob -nocomplain test.db-journal/*]
} {test.db-journal/0-journal test.db-journal/1-journal}

do_test 2.6 {
  execsql {BEGIN}
  execsql {INSERT INTO t1 VALUES(5, 6)}

  execsql {BEGIN} db2
  catchsql {INSERT INTO t1 VALUES(7, 8)} db2
} {1 {database is locked}}
do_test 2.7 {
  # Transaction is automatically rolled back in this case.
  sqlite3_get_autocommit db2
} {1}
do_test 2.8 {
  execsql COMMIT
  execsql { SELECT * FROM t1 } db2
} {1 2 5 6}
db2 close

#-------------------------------------------------------------------------
#
server_reset_db
do_execsql_test 3.0 {
  CREATE TABLE t1(a, b);
}

do_test 3.1 {
  lsort [glob -nocomplain test.db-journal/*]
} {test.db-journal/0-journal}

do_test 3.2 {
  db close
  lsort [glob -nocomplain test.db-journal/*]
} {}
finish_test

