#
# 09-May-2022
#
#-------------------------------------------------------------------------
#
source [file join [file dirname [info script]] test_common.tcl]
set testprefix dmn_prefetch1
start_new_daemon 0
bcv_create_container cont1
bcv_upload_new_db cont1 t1.db {
CREATE TABLE x1(x, y);
WITH s(i) AS (
SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<5000
)
INSERT INTO x1 SELECT randomblob(100), randomblob(100) FROM s;
}
set cmd [bcvfs_create testdir.0 bcvfs]
bcvfs_attach $cmd cont1
sqlite3 db /cont1/t1.db -vfs bcvfs
sqlite3_bcvfs_register_vtab db
do_execsql_test 1.0 {
PRAGMA bcv_client = 'myclient';
SELECT container, database, nblock, ncache FROM bcv_database
} {
myclient
cont1 t1.db 9 1
}
do_test 1.1 {
$cmd prefetch_new pf cont1 t1.db
pf run 4 10000
} {0}
wait_ms 500
do_execsql_test 1.2 {
SELECT container, database, nblock, ncache FROM bcv_database
} {
cont1 t1.db 9 5
}
do_test 1.3a { pf run 0 1000 } {0}
do_execsql_test 1.3b {
SELECT container, database, nblock, ncache FROM bcv_database
} {
cont1 t1.db 9 5
}
do_test 1.4 { pf run 10 1000 } {0}
wait_ms 500
do_test 1.5 { pf run 10 1000 } {101}
pf destroy
do_execsql_test 1.6 {
SELECT container, database, nblock, ncache FROM bcv_database
} {
cont1 t1.db 9 9
}
do_execsql_test 1.7 {
SELECT id, uri LIKE '%manifest%', client, logmsg FROM bcv_http_log
WHERE end_time IS NOT NULL;
} {
1 1 "" ""
2 0 myclient "demand 0 of t1.db"
3 0 prefetch "prefetch 1 of t1.db"
4 0 prefetch "prefetch 2 of t1.db"
5 0 prefetch "prefetch 3 of t1.db"
6 0 prefetch "prefetch 4 of t1.db"
7 0 prefetch "prefetch 5 of t1.db"
8 0 prefetch "prefetch 6 of t1.db"
9 0 prefetch "prefetch 7 of t1.db"
10 0 prefetch "prefetch 8 of t1.db"
}
do_test 1.8 {
$cmd prefetch_new pf cont1 t3.db
list [pf errcode] [pf errmsg]
} {14 {no such database: /cont1/t3.db}}
pf destroy
do_test 1.9 {
$cmd prefetch_new pf cont4 t3.db
list [pf errcode] [pf errmsg]
} {14 {no such container: cont4}}
pf destroy
db close
$cmd destroy
wait_daemon 0
#-------------------------------------------------------------------------
if {[info exists C(sas_cmd)]} {
start_new_daemon 0
set cmd [bcvfs_create testdir.0 bcvfs]
bcvfs_attach $cmd cont1
sqlite3 db /cont1/t1.db -vfs bcvfs
do_execsql_test 2.0 {
SELECT count(*) FROM sqlite_schema
} {1}
proc bin_append_int32 {in int32} {
return "$in[binary format I $int32]"
}
proc bin_append_string {in str} {
set in [bin_append_int32 $in [string length $str]]
return "$in$str"
}
proc send_msg {socket type body} {
set msg "[binary format I $type][binary format I [string length $body]]$body"
puts -nonewline $socket $msg
flush $socket
}
proc read_portnumber {dir} {
set fd [open [file join $dir portnumber.bcv]]
set data [gets $fd]
close $fd
foreach {ip port} [split $data :] {}
return $port
}
# Open up a socket to the daemon process
#
set port [read_portnumber testdir.0]
puts "connecting to $port"
set s [socket 127.0.0.1 $port]
fconfigure $s -encoding binary
fconfigure $s -translation binary
# Send HELLO message
#
set msg [bin_append_string "" cont1]
set msg [bin_append_string $msg t1.db]
set msg [bin_append_int32 $msg 1]
send_msg $s 1 $msg
# Send PREFETCH message
#
set msg [bin_append_string "" [$C(sas_cmd) 0 cont1]]
set msg [bin_append_int32 $msg 32]
set msg [bin_append_int32 $msg 10000]
send_msg $s 14 $msg
# Close socket abruptly
#
close $s
wait_ms 500
db close
$cmd destroy
wait_daemon 0
}
finish_test