hammerwork 1.15.5

A high-performance, database-driven job queue for Rust with PostgreSQL and MySQL support, featuring job prioritization, cron scheduling, event streaming (Kafka/Kinesis/PubSub), webhooks, rate limiting, Prometheus metrics, and comprehensive monitoring
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# Integration Testing Guide

This guide covers how to run and maintain integration tests for the Hammerwork job queue library.

## Overview

Hammerwork includes comprehensive integration testing infrastructure that validates:

- ✅ Job queue functionality with real databases
- ✅ PostgreSQL and MySQL database compatibility
- ✅ Worker pool management and job processing
- ✅ Performance benchmarks and regression testing
- ✅ Containerized deployment scenarios
- ✅ End-to-end job lifecycle management

## Quick Start

### Prerequisites

- **Docker & Docker Compose**: For database containers
- **Rust 1.75+**: For building and running tests
- **Make** (optional): For convenient command execution

### Run All Tests Locally

```bash
# Start databases and run all integration tests
make integration-all

# Or using the script directly
./scripts/test-local.sh
```

### Run Specific Database Tests

```bash
# PostgreSQL only
make integration-postgres
# or
./scripts/test-postgres.sh

# MySQL only
make integration-mysql
# or
./scripts/test-mysql.sh
```

## Test Infrastructure

### Directory Structure

```
hammerwork/
├── docker-compose.yml           # Database containers
├── Makefile                     # Convenient commands
├── scripts/
│   ├── test-local.sh           # Main test runner
│   ├── test-postgres.sh        # PostgreSQL tests
│   ├── test-mysql.sh           # MySQL tests
│   ├── setup-db.sh             # Database management
│   └── cleanup.sh              # Environment cleanup
├── config/
│   ├── init-postgres.sql       # PostgreSQL initialization
│   └── init-mysql.sql          # MySQL initialization
├── integrations/
│   ├── shared/
│   │   └── test_scenarios.rs   # Common test scenarios
│   ├── postgres-integration/   # PostgreSQL-specific tests
│   └── mysql-integration/      # MySQL-specific tests
└── .github/workflows/
    └── integration.yml          # CI/CD pipeline
```

### Test Categories

1. **Unit Tests**: Fast tests without external dependencies
2. **Integration Tests**: Database-backed functionality tests
3. **Performance Tests**: Benchmarking and regression detection
4. **Docker Tests**: Containerized deployment validation

## Local Development

### Database Management

```bash
# Start database containers
make start-db

# Check database status
make status-db

# View database logs
make logs-db

# Stop databases
make stop-db

# Clean up everything
make clean-db
```

### Running Tests

```bash
# Quick development cycle
make quick              # format + check + unit tests

# Full development cycle
make full               # clean + build + lint + all tests

# Unit tests only
make test-unit

# Integration tests (no database)
make test-integration

# Performance benchmarks
make performance
```

### Test Options

The main test script supports several options:

```bash
./scripts/test-local.sh --help

Options:
  --unit-only         Run only unit tests
  --postgres-only     Run only PostgreSQL integration tests
  --mysql-only        Run only MySQL integration tests
  --performance       Run performance benchmarks
  --no-cleanup        Don't cleanup containers after tests
  --help              Show help message
```

## Test Scenarios

### Core Functionality Tests

Each integration application runs these shared test scenarios:

1. **Basic Job Lifecycle**
   - Job creation, enqueueing, dequeuing
   - Status transitions and completion
   - Database consistency verification

2. **Delayed Jobs**
   - Future scheduling functionality
   - Proper timing behavior
   - Schedule-based job availability

3. **Job Retries**
   - Failure handling and retry logic
   - Max attempts enforcement
   - Error message persistence

4. **Worker Pool Management**
   - Multiple worker coordination
   - Concurrent job processing
   - Graceful shutdown handling

5. **Concurrent Processing**
   - Race condition prevention
   - Database locking behavior
   - Transaction isolation

6. **Error Handling**
   - Edge case management
   - Graceful failure modes
   - Data consistency preservation

### Database-Specific Tests

#### PostgreSQL Tests

- **JSONB Operations**: Complex JSON payload handling
- **FOR UPDATE SKIP LOCKED**: Concurrent job dequeuing
- **Advanced Indexing**: Query performance optimization
- **Transaction Semantics**: PostgreSQL-specific behavior

#### MySQL Tests

- **JSON Operations**: MySQL JSON datatype handling
- **Transaction Isolation**: MySQL locking behavior
- **Character Encoding**: Unicode and special character support
- **Performance Characteristics**: MySQL-specific optimizations

## Performance Benchmarking

### Running Benchmarks

```bash
# Run performance tests
make benchmark

# Or with specific databases
./scripts/test-local.sh --performance
```

### Performance Metrics

Tests measure and validate:

- **Enqueue Rate**: Jobs per second insertion
- **Dequeue Rate**: Jobs per second retrieval
- **Processing Latency**: End-to-end job processing time
- **Memory Usage**: Resource utilization patterns
- **Concurrent Throughput**: Multi-worker performance

### Performance Assertions

- PostgreSQL: > 10 jobs/sec enqueue/dequeue
- MySQL: > 5 jobs/sec enqueue/dequeue
- Memory: Stable usage patterns
- Latency: < 100ms for basic operations

## Continuous Integration

### GitHub Actions Workflow

The CI pipeline (`.github/workflows/integration.yml`) runs:

1. **Unit Tests**: Fast validation without dependencies
2. **PostgreSQL Integration**: Full database testing
3. **MySQL Integration**: MySQL-specific validation
4. **Docker Tests**: Containerized scenario testing
5. **Security Audit**: Vulnerability scanning
6. **Performance Tests**: Benchmark execution (scheduled)

### Triggers

- **Push/PR**: On master/main/develop branches
- **Scheduled**: Daily performance benchmarks
- **Manual**: `[benchmark]` in commit message

### Test Matrix

| Job | Database | Purpose |
|-----|----------|---------|
| unit-tests | None | Fast validation |
| postgres-integration | PostgreSQL 16 | Feature testing |
| mysql-integration | MySQL 8.0 | Feature testing |
| docker-tests | Both | Container validation |
| performance-tests | Both | Benchmark regression |

## Docker Testing

### Building Images

```bash
# Build integration images
make docker-build

# Run specific integrations
make docker-run-postgres
make docker-run-mysql
```

### Container Configuration

Integration containers are configured with:
- Minimal runtime dependencies
- Non-root user execution
- Health checks for monitoring
- Proper logging configuration

## Troubleshooting

### Common Issues

#### Database Connection Failures

```bash
# Check database status
make status-db

# View database logs
make logs-db

# Restart databases
make restart-db
```

#### Test Timeouts

```bash
# Increase timeout in scripts
export TEST_TIMEOUT=600  # 10 minutes

# Run with verbose logging
export RUST_LOG=debug
./scripts/test-local.sh
```

#### Container Issues

```bash
# Full cleanup and restart
make cleanup
make dev-setup

# Check Docker resources
docker system df
docker system prune
```

### Debugging Tests

1. **Enable Debug Logging**:
   ```bash
   export RUST_LOG=hammerwork=debug
   ./scripts/test-local.sh
   ```

2. **Run Individual Test Scenarios**:
   ```bash
   # Modify integration apps to run specific tests
   cargo run --bin postgres-integration --features postgres
   ```

3. **Inspect Database State**:
   ```bash
   # PostgreSQL
   docker exec -it hammerwork-postgres psql -U postgres -d hammerwork_test
   
   # MySQL
   docker exec -it hammerwork-mysql mysql -u hammerwork -ppassword hammerwork_test
   ```

## Adding New Tests

### Creating New Test Scenarios

1. **Add to Shared Scenarios** (`integrations/shared/test_scenarios.rs`):
   ```rust
   impl<DB> TestScenarios<DB> {
       pub async fn test_new_feature(&self) -> Result<()> {
           // Your test implementation
       }
   }
   ```

2. **Update Integration Applications**:
   - Add feature-specific tests to `postgres-integration/src/main.rs`
   - Add feature-specific tests to `mysql-integration/src/main.rs`

3. **Update CI Pipeline**:
   - Add new test validation to `.github/workflows/integration.yml`

### Database Schema Changes

1. **Update Initialization Scripts**:
   - Modify `config/init-postgres.sql`
   - Modify `config/init-mysql.sql`

2. **Update Database Queue Implementations**:
   - Modify PostgreSQL implementation in `src/queue.rs`
   - Modify MySQL implementation in `src/queue.rs`

3. **Add Migration Tests**:
   - Test schema compatibility
   - Validate data migration procedures

## Best Practices

### Test Development

- ✅ Write tests for both happy path and edge cases
- ✅ Include cleanup logic to prevent test pollution
- ✅ Use descriptive test names and logging
- ✅ Test database-specific behaviors separately
- ✅ Validate performance regression prevention

### Database Testing

- ✅ Use fresh database instances for each test run
- ✅ Test transaction rollback scenarios
- ✅ Validate data consistency across operations
- ✅ Test concurrent access patterns
- ✅ Include schema migration validation

### CI/CD Integration

- ✅ Keep test execution time reasonable (< 10 minutes)
- ✅ Use appropriate timeouts for reliability
- ✅ Cache dependencies for faster builds
- ✅ Provide clear failure reporting
- ✅ Include performance regression detection

## Environment Variables

### Required for Testing

```bash
# Database connections
DATABASE_URL=postgres://postgres:password@localhost:5432/hammerwork_test
# or
DATABASE_URL=mysql://hammerwork:password@localhost:3306/hammerwork_test

# Logging configuration
RUST_LOG=info                    # General logging
RUST_BACKTRACE=1                 # Error backtraces

# Test configuration
TEST_TIMEOUT=300                 # Test timeout in seconds
INTEGRATION_TEST_MODE=true       # Enable integration test mode
```

### Optional Configuration

```bash
# Performance testing
PERFORMANCE_TEST_ITERATIONS=100  # Number of test jobs
BENCHMARK_MODE=true              # Enable benchmark mode

# CI-specific
CI=true                          # CI environment flag
GITHUB_ACTIONS=true              # GitHub Actions specific
```

This integration testing infrastructure ensures Hammerwork maintains high quality and reliability across different database backends and deployment scenarios.