Skip to content

Commit

Permalink
fix(sqlite): fix off-by-one in max number of active connections (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
timvandam committed Dec 3, 2022
1 parent 2bb6ef1 commit d7890e1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/sqlite/src/sqlite-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class SQLiteConnectionPool extends SQLConnectionPool {
}

const connection = this.firstConnection && this.firstConnection.released ? this.firstConnection :
this.activeConnections > this.maxConnections
this.activeConnections >= this.maxConnections
//we wait for the next query to be released and reuse it
? await asyncOperation<SQLiteConnection>((resolve) => {
this.queue.push(resolve);
Expand Down
30 changes: 29 additions & 1 deletion packages/sqlite/tests/sqlite.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect, test } from '@jest/globals';
import { SQLitePlatform } from '../src/sqlite-platform';
import { databaseFactory } from './factory';
import { User, UserCredentials } from '@deepkit/orm-integration';
import { SQLiteDatabaseAdapter, SQLiteDatabaseTransaction } from '../src/sqlite-adapter';
import { SQLiteConnection, SQLiteDatabaseAdapter, SQLiteDatabaseTransaction } from '../src/sqlite-adapter';
import { sleep } from '@deepkit/core';
import { AutoIncrement, cast, Entity, entity, PrimaryKey, Reference, ReflectionClass, serialize, typeOf, UUID, uuid } from '@deepkit/type';
import { Database, DatabaseEntityRegistry } from '@deepkit/orm';
Expand Down Expand Up @@ -229,6 +229,8 @@ test('connection pool', async () => {

const c3 = await sqlite.connectionPool.getConnection();
expect(c3 === c1).toBe(true);

c3.release();
}

{
Expand Down Expand Up @@ -265,6 +267,32 @@ test('connection pool', async () => {
}
});

test(':memory: connection pool', async () => {
const sqlite = new SQLiteDatabaseAdapter(':memory:');

// create a connection, or return null if it takes longer than 100 ms
const createConnectionOrNull = () => Promise.race([
sqlite.connectionPool.getConnection(),
sleep(0.1).then(() => null),
])

{
const c1 = await sqlite.connectionPool.getConnection();
const c2 = await createConnectionOrNull();
const c3 = await createConnectionOrNull();

expect(sqlite.connectionPool.getActiveConnections()).toBeLessThanOrEqual(sqlite.connectionPool.maxConnections);

expect(c1).toBeDefined();
expect(c2).toBeNull();
expect(c3).toBeNull();

c1.release();
c2?.release();
c3?.release();
}
});

test('optional', async () => {
@entity.name('entity')
class MyEntity {
Expand Down

0 comments on commit d7890e1

Please sign in to comment.