-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring LightBlockUpload and writing tests
- Loading branch information
Showing
10 changed files
with
83 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,50 @@ | ||
import { LightBlockUpload } from "./index"; | ||
import { lightBlockCache } from "../cache"; | ||
import { lightBlockUpload } from "./index"; | ||
import { | ||
S3Client, | ||
PutObjectCommand, | ||
ListObjectsV2Command, | ||
S3Client, | ||
} from "@aws-sdk/client-s3"; | ||
import { LightBlockCache } from "../cache"; | ||
import { LightBlock } from "../models/lightstreamer"; | ||
|
||
jest.mock("@aws-sdk/client-s3", () => { | ||
return { | ||
S3Client: jest.fn().mockImplementation(() => { | ||
return { | ||
send: jest.fn().mockImplementation(() => { | ||
return { Contents: [] }; | ||
}), | ||
}; | ||
}), | ||
ListObjectsV2Command: jest.fn().mockImplementation(() => { | ||
return {}; | ||
}), | ||
PutObjectCommand: jest.fn().mockImplementation(() => { | ||
return {}; | ||
}), | ||
}; | ||
}); | ||
|
||
describe("LightBlockUpload", () => { | ||
let lightBlockUpload: LightBlockUpload; | ||
let mockCache: jest.Mocked<LightBlockCache>; | ||
let mockS3Client: jest.Mocked<S3Client>; | ||
|
||
beforeAll(() => { | ||
mockCache = new LightBlockCache() as jest.Mocked<LightBlockCache>; | ||
mockS3Client = new S3Client({}) as jest.Mocked<S3Client>; | ||
lightBlockUpload = new LightBlockUpload(mockCache); | ||
jest.spyOn(S3Client.prototype, "send").mockImplementation((command) => { | ||
if (command instanceof ListObjectsV2Command) { | ||
return Promise.resolve({ | ||
Contents: [ | ||
{ Key: lightBlockUpload.uploadName({ start: 1, end: 1000 }) }, | ||
{ Key: lightBlockUpload.uploadName({ start: 1001, end: 2000 }) }, | ||
], | ||
}); | ||
} else if (command instanceof PutObjectCommand) { | ||
return Promise.resolve({ | ||
/* your mock PutObjectCommand response */ | ||
}); | ||
} else { | ||
throw new Error( | ||
`Command mock not implemented: ${command.constructor.name}`, | ||
); | ||
} | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
afterAll(async () => { | ||
jest.resetAllMocks(); | ||
await lightBlockCache.close(); | ||
}); | ||
|
||
it("should throw an error if environment variables are not set", () => { | ||
delete process.env["BUCKET_ENDPOINT"]; | ||
expect(() => new LightBlockUpload(mockCache)).toThrow(); | ||
it("upload name creation should be reversible", () => { | ||
const blockRange = { start: 1, end: 1000 }; | ||
const key = lightBlockUpload.uploadName(blockRange); | ||
const newBlockRange = lightBlockUpload.parseUploadName(key); | ||
expect(blockRange).toEqual(newBlockRange); | ||
}); | ||
|
||
it("should upload blocks", async () => { | ||
const mockBlock = LightBlock.fromJSON({ | ||
sequence: 1000, | ||
hash: "test-hash", | ||
previousBlockHash: "test-previous-hash", | ||
timestamp: 123456789, | ||
transactions: [], | ||
noteSize: 0, | ||
}); | ||
mockCache.getBlockBySequence.mockResolvedValue(mockBlock); | ||
mockCache.getHeadSequence.mockResolvedValue(1001); | ||
mockCache.getUploadHead.mockResolvedValue(0); | ||
|
||
const mockSend = jest.fn(); | ||
mockS3Client.send = mockSend; | ||
|
||
await lightBlockUpload.watchAndUpload(); | ||
|
||
expect(mockSend).toHaveBeenCalledWith(expect.any(ListObjectsV2Command)); | ||
expect(mockSend).toHaveBeenCalledWith(expect.any(PutObjectCommand)); | ||
expect(mockCache.putUploadHead).toHaveBeenCalledWith("test-hash"); | ||
it("existing uploads should return block ranges", async () => { | ||
const ranges = await lightBlockUpload.existingUploads(); | ||
expect(ranges).toEqual([ | ||
{ start: 1, end: 1000 }, | ||
{ start: 1001, end: 2000 }, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
MANIFEST-000231 | ||
MANIFEST-000122 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
2024/03/19-09:37:17.297631 173e2b000 Recovering log #121 | ||
2024/03/19-09:37:17.298491 173e2b000 Delete type=3 #119 | ||
2024/03/19-09:37:17.298537 173e2b000 Delete type=0 #121 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
2024/03/20-17:13:07.794174 171fcb000 Recovering log #230 | ||
2024/03/20-17:13:07.795044 171fcb000 Delete type=3 #229 | ||
2024/03/20-17:13:07.795090 171fcb000 Delete type=0 #230 | ||
2024/03/19-09:37:16.985331 172e13000 Recovering log #118 | ||
2024/03/19-09:37:16.985449 172e13000 Level-0 table #120: started | ||
2024/03/19-09:37:16.985657 172e13000 Level-0 table #120: 1766 bytes OK | ||
2024/03/19-09:37:16.986076 172e13000 Delete type=3 #117 | ||
2024/03/19-09:37:16.986118 172e13000 Delete type=0 #118 |
Binary file not shown.