1--TEST--
2mysqli_pconnect() - reusing/caching persistent connections - TODO
3--SKIPIF--
4<?php
5die("skip TODO - we need to add a user level way to check if CHANGE_USER gets called by pconnect");
6
7require_once('skipif.inc');
8require_once('skipifconnectfailure.inc');
9?>
10--INI--
11mysqli.allow_persistent=1
12mysqli.max_persistent=2
13mysqli.max_links=2
14--FILE--
15<?php
16    require_once("connect.inc");
17
18    $host = 'p:' . $host;
19    if (!$link1 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
20        printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s, [%d] %s\n",
21            $host, $user, $db, $port, $socket, mysqli_connect_errno(), mysqli_connect_error());
22    }
23    if (!mysqli_query($link1, 'SET @pcondisabled = "Connection 1"'))
24        printf("[002] Cannot set user variable to check if we got the same persistent connection, [%d] %s\n",
25            mysqli_errno($link1), mysqli_error($link1));
26
27    if (!$link2 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
28        printf("[003] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s, [%d] %s\n",
29            $host, $user, $db, $port, $socket, mysqli_connect_errno(), mysqli_connect_error());
30    }
31
32    if (!$res = mysqli_query($link1, 'SELECT @pcondisabled AS _test'))
33        printf("[004] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
34
35    $row = mysqli_fetch_assoc($res);
36    printf("Connection 1 - SELECT @pcondisabled -> '%s'\n", $row['_test']);
37    mysqli_free_result($res);
38
39    if (!$res = mysqli_query($link2, 'SELECT @pcondisabled AS _test'))
40        printf("[005] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
41
42    $row = mysqli_fetch_assoc($res);
43    printf("Connection 2 (no reuse) - SELECT @pcondisabled -> '%s'\n", $row['_test']);
44    $thread_id = mysqli_thread_id($link2);
45    printf("Connection 2 (no reuse) - Thread ID -> '%s'\n", $thread_id);
46    mysqli_free_result($res);
47
48    if (!mysqli_query($link2, 'SET @pcondisabled = "Connection 2"'))
49        printf("[006] Cannot set user variable to check if we got the same persistent connection, [%d] %s\n",
50            mysqli_errno($link2), mysqli_error($link2));
51
52    if (!$res = mysqli_query($link2, 'SELECT @pcondisabled AS _test'))
53        printf("[007] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
54
55    $row = mysqli_fetch_assoc($res);
56    printf("Connection 2 - SELECT @pcondisabled -> '%s'\n", $row['_test']);
57    mysqli_free_result($res);
58
59    mysqli_close($link2);
60
61    /* reuse of existing persistent connection expected! */
62    if (!$link2 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
63        printf("[008] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s, [%d] %s\n",
64            $host, $user, $db, $port, $socket, mysqli_connect_errno(), mysqli_connect_error());
65    }
66
67    if (!$res = mysqli_query($link2, 'SELECT @pcondisabled AS _test'))
68        printf("[009] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
69
70    $row = mysqli_fetch_assoc($res);
71    printf("Connection 2 (reuse) - SELECT @pcondisabled -> '%s'\n", $row['_test']);
72    $thread_id_reuse = mysqli_thread_id($link2);
73    printf("Connection 2 (reuse) - Thread ID -> '%s'\n", $thread_id_reuse);
74    mysqli_free_result($res);
75
76    if ($thread_id != $thread_id_reuse)
77        printf("[010] Seems as if we have got a new connection, connections should have been cached and reused!\n");
78
79    mysqli_close($link1);
80    mysqli_close($link2);
81    print "done!";
82?>
83--EXPECTF--
84Connection 1 - SELECT @pcondisabled -> 'Connection 1'
85Connection 2 (no reuse) - SELECT @pcondisabled -> ''
86Connection 2 (no reuse) - Thread ID -> '%d'
87Connection 2 - SELECT @pcondisabled -> 'Connection 2'
88Connection 2 (reuse) - SELECT @pcondisabled -> 'Connection 2'
89Connection 2 (reuse) - Thread ID -> '%d'
90done!
91