Skip to content

Commit 4046155

Browse files
authored
Implement whitebox testing of constructor arguments (#24)
Implement a framework for retrieving the protobuf ConnectionRequest message that the PHP ValkeyGlide and ValkeyGlideCluster constructors generate. Add tests to validate various command inputs by examining the message deserialized into PHP protobuf classes. Fix various bugs: * When using multiple addresses only the first address was used. * Reconnection strategy was not being used. * Client AZ was not being used. * Connection timeout was not being used. * Disabling periodic checks did not disable it correctly. * Made defaults for connection timeout and reconnection strategy consistent with Rust client. Integrate tests into CI: * Register the test class * Install PHP dependencies through composer in CI * Generate PHP protobuf objects in CI * Add dependency on symfony-polyfill to resolve issues finding ctype_* functions Signed-off-by: James Duong <[email protected]>
1 parent c51138c commit 4046155

22 files changed

+899
-88
lines changed

.github/workflows/build-php-wrapper/action.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ runs:
4545
libtool \
4646
pkg-config \
4747
libssl-dev \
48+
php-bcmath \
4849
unzip \
4950
clang-format \
5051
cppcheck \

.github/workflows/php.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ jobs:
140140
github-token: ${{ secrets.GITHUB_TOKEN }}
141141
engine-version: ${{ matrix.engine.version }}
142142

143+
- name: Generate test protobuf PHP classes
144+
run: |
145+
protoc --proto_path=./valkey-glide/glide-core/src/protobuf --php_out=./tests/ ./valkey-glide/glide-core/src/protobuf/connection_request.proto
146+
echo "tests directory contents:"
147+
cd tests
148+
ls -la
149+
150+
- name: Install PHP dependencies
151+
run: |
152+
composer install --no-interaction --prefer-dist --optimize-autoloader
153+
echo "Composer install completed. Checking for composer.lock:"
154+
ls -la composer.lock || echo "Warning: composer.lock not found"
155+
echo "Current directory contents:"
156+
ls -la
157+
143158
- name: Start Valkey servers
144159
working-directory: tests
145160
run: |

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,23 @@ src/**
8181
include/**
8282
lib/**
8383
.libs/**
84+
tests/.libs/**
8485
*.dep
8586
*.lo
8687
valkey_glide.la
8788
valkey_glide_arginfo.h
8889
valkey_glide_cluster_arginfo.h
8990
valkey_glide_cluster_legacy_arginfo.h
9091
valkey_glide_legacy_arginfo.h
92+
logger_arginfo.h
93+
logger_legacy_arginfo.h
9194
run-tests.php
9295
modules/valkey_glide.so
9396
libtool
9497
cluster_scan_cursor_legacy_arginfo.h
9598
cluster_scan_cursor_arginfo.h
9699
tests/valkey_data/*/valkey.log
100+
composer.lock
97101
configure
98102
configure.ac
99103
config.h.in
@@ -106,3 +110,8 @@ Makefile.objects
106110
autom4te.cache
107111
config.h.in~
108112
configure~
113+
tests/Connection_request/**
114+
tests/GPBMetadata/**
115+
tests/client_constructor_mock_arginfo.h
116+
tests/client_constructor_mock_legacy_arginfo.h
117+
vendor/**

DEVELOPER.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Continue with **Install protobuf compiler** and **Install `ziglang` and `zigbuil
5555

5656
```bash
5757
sudo yum update -y
58-
sudo yum install -y php-devel php-cli git gcc make autoconf automake libtool pkgconfig openssl openssl-devel unzip
58+
sudo yum install -y php-devel php-cli git gcc make autoconf automake libtool pkgconfig openssl openssl-devel unzip php-bcmath
5959
# Install rust
6060
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
6161
source "$HOME/.cargo/env"
@@ -190,6 +190,8 @@ Before starting this step, make sure you've installed all software requirements.
190190
To run unit tests, use:
191191

192192
```bash
193+
protoc --proto_path=./valkey-glide/glide-core/src/protobuf --php_out=./tests/ ./valkey-glide/glide-core/src/protobuf/connection_request.proto
194+
composer install --no-interaction --prefer-dist --optimize-autoloader
193195
make install
194196
cd tests
195197
./start_valkey_with_replicas.sh

Makefile.frag

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,17 @@ cluster_scan_cursor_arginfo.h: cluster_scan_cursor.stub.php
7474
@echo "Generating arginfo from cluster_scan_cursor.stub.php"
7575
$(PHP_EXECUTABLE) build/gen_stub.php --no-legacy-arginfo cluster_scan_cursor.stub.php
7676

77-
ARGINFO_HEADERS = valkey_glide_arginfo.h valkey_glide_cluster_arginfo.h cluster_scan_cursor_arginfo.h logger_arginfo.h
77+
tests/client_constructor_mock_arginfo.h: tests/client_constructor_mock.stub.php
78+
@echo "Generating arginfo from tests/client_constructor_mock_arginfo.stub.php"
79+
$(PHP_EXECUTABLE) build/gen_stub.php --no-legacy-arginfo tests/client_constructor_mock.stub.php
80+
81+
ARGINFO_HEADERS = valkey_glide_arginfo.h valkey_glide_cluster_arginfo.h cluster_scan_cursor_arginfo.h logger_arginfo.h tests/client_constructor_mock_arginfo.h
7882

7983
all: $(ARGINFO_HEADERS)
8084

8185
.PHONY: build-modules-pre
8286

83-
build-modules-pre: valkey_glide_arginfo.h valkey_glide_cluster_arginfo.h cluster_scan_cursor_arginfo.h logger_arginfo.h
87+
build-modules-pre: valkey_glide_arginfo.h valkey_glide_cluster_arginfo.h cluster_scan_cursor_arginfo.h logger_arginfo.h tests/client_constructor_mock_arginfo.h
8488
@$(MAKE) generate-proto
8589
@$(MAKE) generate-bindings
8690

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ Before installing Valkey GLIDE PHP extension, ensure you have the following depe
6868
- protoc (protobuf compiler) >= v3.20.0
6969
- openssl and openssl-dev
7070
- rustup (Rust toolchain)
71+
- php-bcmath (Protobuf PHP dependency, needed only for testing)
7172

7273
### Installing Dependencies
7374

7475
**Ubuntu/Debian:**
7576
```bash
7677
sudo apt update -y
77-
sudo apt install -y php-dev php-cli git gcc make autotools-dev pkg-config openssl libssl-dev unzip
78+
sudo apt install -y php-dev php-cli git gcc make autotools-dev pkg-config openssl libssl-dev unzip php-bcmath
7879
# Install rust
7980
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
8081
source "$HOME/.cargo/env"
@@ -83,7 +84,7 @@ source "$HOME/.cargo/env"
8384
**CentOS/RHEL:**
8485
```bash
8586
sudo yum update -y
86-
sudo yum install -y php-devel php-cli git gcc make pkgconfig openssl openssl-devel unzip
87+
sudo yum install -y php-devel php-cli git gcc make pkgconfig openssl openssl-devel unzip php-bcmath
8788
# Install rust
8889
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
8990
source "$HOME/.cargo/env"
@@ -150,8 +151,18 @@ protoc --version
150151
```ini
151152
extension=valkey_glide
152153
```
153-
154-
6. Execute the tests:
154+
155+
6. Generate PHP protobuf classes used for testing purposes:
156+
```bash
157+
protoc --proto_path=./valkey-glide/glide-core/src/protobuf --php_out=./tests/ ./valkey-glide/glide-core/src/protobuf/connection_request.proto
158+
```
159+
160+
7. Install PHP dependencies with composer:
161+
```bash
162+
composer install --no-interaction --prefer-dist --optimize-autoloader
163+
```
164+
165+
8. Execute the tests:
155166
```
156167
make test
157168
```

common.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,28 @@ typedef struct {
6767
char* username; /* Optional */
6868
} valkey_glide_server_credentials_t;
6969

70+
/* Default values for connection configuration options. */
71+
#define VALKEY_GLIDE_DEFAULT_NUM_OF_RETRIES 5
72+
#define VALKEY_GLIDE_DEFAULT_FACTOR 100
73+
#define VALKEY_GLIDE_DEFAULT_EXPONENT_BASE 2
74+
#define VALKEY_GLIDE_DEFAULT_JITTER_PERCENTAGE 20
75+
76+
#define VALKEY_GLIDE_DEFAULT_CONNECTION_TIMEOUT 250
77+
78+
7079
typedef struct {
7180
int num_of_retries;
7281
int factor;
7382
int exponent_base;
74-
int jitter_percent; /* -1 if not set */
83+
int jitter_percent;
7584
} valkey_glide_backoff_strategy_t;
7685

7786
typedef struct {
7887
bool use_insecure_tls; /* false if not set */
7988
} valkey_glide_tls_advanced_configuration_t;
8089

8190
typedef struct {
82-
int connection_timeout; /* -1 if not set */
91+
int connection_timeout; /* In milliseconds. */
8392
valkey_glide_tls_advanced_configuration_t* tls_config; /* NULL if not set */
8493
} valkey_glide_advanced_base_client_configuration_t;
8594

composer.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@
1515
},
1616
"require-dev": {
1717
"squizlabs/php_codesniffer": "^3.7",
18-
"phpstan/phpstan": "^1.10"
18+
"phpstan/phpstan": "^1.10",
19+
"google/protobuf": "^v4.32.0",
20+
"protobuf-php/protobuf-plugin": "^0.1.3",
21+
"symfony/polyfill-ctype": "^1.32.0"
1922
},
2023
"autoload": {
2124
"psr-4": {
2225
"ValkeyGlide\\": "src/"
2326
}
2427
},
25-
"autoload-dev": {
26-
"psr-4": {
27-
"ValkeyGlide\\Tests\\": "tests/"
28-
}
29-
},
3028
"scripts": {
3129
"lint": [
3230
"phpcs --standard=phpcs.xml"

config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ if test "$PHP_VALKEY_GLIDE" != "no"; then
5252
LDFLAGS="$LDFLAGS $PHP_VALKEY_GLIDE_LDFLAGS"
5353
fi
5454
PHP_NEW_EXTENSION(valkey_glide,
55-
valkey_glide.c valkey_glide_cluster.c cluster_scan_cursor.c command_response.c logger.c valkey_glide_commands.c valkey_glide_commands_2.c valkey_glide_commands_3.c valkey_glide_core_commands.c valkey_glide_core_common.c valkey_glide_expire_commands.c valkey_glide_geo_commands.c valkey_glide_geo_common.c valkey_glide_hash_common.c valkey_glide_list_common.c valkey_glide_s_common.c valkey_glide_str_commands.c valkey_glide_x_commands.c valkey_glide_x_common.c valkey_glide_z.c valkey_glide_z_common.c valkey_z_php_methods.c src/command_request.pb-c.c src/connection_request.pb-c.c src/response.pb-c.c,
55+
valkey_glide.c valkey_glide_cluster.c cluster_scan_cursor.c command_response.c logger.c valkey_glide_commands.c valkey_glide_commands_2.c valkey_glide_commands_3.c valkey_glide_core_commands.c valkey_glide_core_common.c valkey_glide_expire_commands.c valkey_glide_geo_commands.c valkey_glide_geo_common.c valkey_glide_hash_common.c valkey_glide_list_common.c valkey_glide_s_common.c valkey_glide_str_commands.c valkey_glide_x_commands.c valkey_glide_x_common.c valkey_glide_z.c valkey_glide_z_common.c valkey_z_php_methods.c src/command_request.pb-c.c src/connection_request.pb-c.c src/response.pb-c.c tests/client_constructor_mock.c,
5656
$ext_shared)
5757

5858
EXTRA_DIST="$EXTRA_DIST valkey_glide.stub.php valkey_glide_cluster.stub.php logger.stub.php"

php_build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ cd valkey-glide/ffi
77
cargo build --release
88
cd ../../
99

10+
protoc --proto_path=./valkey-glide/glide-core/src/protobuf --php_out=./tests/generated ./valkey-glide/glide-core/src/protobuf/connection_request.proto
11+
1012
phpize
1113
./configure --enable-valkey-glide
1214
make -j4 build-modules-pre

0 commit comments

Comments
 (0)