Skip to content

Commit

Permalink
[schema-registry-avro] Migrate to recorder v2 (Azure#24868)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgetu authored Feb 15, 2023
1 parent afc84cf commit 896e9ae
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 83 deletions.
25 changes: 6 additions & 19 deletions sdk/schemaregistry/schema-registry-avro/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
// https://github.com/karma-runner/karma-chrome-launcher
process.env.CHROME_BIN = require("puppeteer").executablePath();
require("dotenv").config();
const {
jsonRecordingFilterFunction,
isPlaybackMode,
isSoftRecordMode,
isRecordMode,
} = require("@azure-tools/test-recorder");
const { relativeRecordingsPath } = require("@azure-tools/test-recorder");

process.env.RECORDINGS_RELATIVE_PATH = relativeRecordingsPath();

module.exports = function (config) {
config.set({
Expand All @@ -31,16 +28,14 @@ module.exports = function (config) {
"karma-coverage",
"karma-sourcemap-loader",
"karma-junit-reporter",
"karma-json-to-file-reporter",
"karma-source-map-support",
"karma-json-preprocessor",
],

// list of files / patterns to load in the browser
files: [
"dist-test/index.browser.js",
{ pattern: "dist-test/index.browser.js.map", type: "html", included: false, served: true },
].concat(isPlaybackMode() || isSoftRecordMode() ? ["recordings/browsers/**/*.json"] : []),
],

// list of files / patterns to exclude
exclude: [],
Expand All @@ -49,7 +44,6 @@ module.exports = function (config) {
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
"**/*.js": ["sourcemap", "env"],
"recordings/browsers/**/*.json": ["json"],
// IMPORTANT: COMMENT following line if you want to debug in your browsers!!
// Preprocess source file to calculate code coverage, however this will make source file unreadable
// "dist-test/index.js": ["coverage"]
Expand All @@ -63,12 +57,13 @@ module.exports = function (config) {
"AZURE_CLIENT_ID",
"AZURE_CLIENT_SECRET",
"AZURE_TENANT_ID",
"RECORDINGS_RELATIVE_PATH",
],

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ["mocha", "coverage", "junit", "json-to-file"],
reporters: ["mocha", "coverage", "junit"],

coverageReporter: {
// specify a common output directory
Expand All @@ -91,11 +86,6 @@ module.exports = function (config) {
properties: {}, // key value pair of properties to add to the <properties> section of the report
},

jsonToFileReporter: {
filter: jsonRecordingFilterFunction,
outputPath: ".",
},

// web server port
port: 9876,

Expand Down Expand Up @@ -130,9 +120,6 @@ module.exports = function (config) {
browserNoActivityTimeout: 600000,
browserDisconnectTimeout: 10000,
browserDisconnectTolerance: 3,
browserConsoleLogOptions: {
terminal: !isRecordMode(),
},

client: {
mocha: {
Expand Down
7 changes: 4 additions & 3 deletions sdk/schemaregistry/schema-registry-avro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"test:browser": "npm run build:test && npm run unit-test:browser && npm run integration-test:browser",
"test:node": "npm run build:test && npm run unit-test:node && npm run integration-test:node",
"test": "npm run build:test && npm run unit-test && npm run integration-test",
"unit-test:browser": "karma start --single-run",
"unit-test:node": "mocha -r esm -r ts-node/register -r source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 1200000 --full-trace \"test/{,!(browser)/**/}*.spec.ts\"",
"unit-test:browser": "dev-tool run test:browser",
"unit-test:node": "dev-tool run test:node-ts-input -- --timeout 1200000 \"test/{,!(browser)/**/}*.spec.ts\"",
"unit-test": "npm run unit-test:node && npm run unit-test:browser"
},
"files": [
Expand Down Expand Up @@ -83,7 +83,8 @@
"@azure/event-hubs": "^5.8.0",
"@azure/identity": "^2.0.1",
"@azure/test-utils": "^1.0.0",
"@azure-tools/test-recorder": "^1.0.0",
"@azure-tools/test-credential": "^1.0.0",
"@azure-tools/test-recorder": "^2.0.0",
"@microsoft/api-extractor": "^7.31.1",
"@rollup/plugin-commonjs": "^24.0.0",
"@rollup/plugin-inject": "^5.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,27 @@ import { Context } from "mocha";
import { MessageContent } from "../../src/";
import chaiPromises from "chai-as-promised";
import { createTestRegistry } from "./utils/mockedRegistryClient";
import { isLive } from "./utils/isLive";
import { v4 as uuid } from "uuid";
import { Recorder, isLiveMode } from "@azure-tools/test-recorder";
import { SchemaRegistry } from "@azure/schema-registry";

chaiUse(chaiPromises);

describe("AvroSerializer", function () {
const noAutoRegisterOptions: CreateTestSerializerOptions<any> = {
serializerOptions: { autoRegisterSchemas: false, groupName: testGroup },
};
describe("AvroSerializer", async function () {
let noAutoRegisterOptions: CreateTestSerializerOptions<any>;
let recorder: Recorder;
let registry: SchemaRegistry;

beforeEach(async function (this: Context) {
recorder = new Recorder(this.currentTest);
registry = createTestRegistry({ recorder });
noAutoRegisterOptions = {
serializerOptions: { autoRegisterSchemas: false, groupName: testGroup },
recorder,
};
});

it("serializes to the expected format", async () => {
const registry = createTestRegistry();
const schemaId = await registerTestSchema(registry);
const serializer = await createTestSerializer<MessageContent>({
...noAutoRegisterOptions,
Expand All @@ -44,7 +53,6 @@ describe("AvroSerializer", function () {
});

it("deserializes from the expected format", async () => {
const registry = createTestRegistry();
const schemaId = await registerTestSchema(registry);
const serializer = await createTestSerializer<MessageContent>({
...noAutoRegisterOptions,
Expand All @@ -61,7 +69,7 @@ describe("AvroSerializer", function () {
});

it("serializes and deserializes in round trip", async () => {
let serializer = await createTestSerializer();
let serializer = await createTestSerializer({ recorder });
let message = await serializer.serialize(testValue, testSchema);
assert.deepStrictEqual(await serializer.deserialize(message), testValue);

Expand All @@ -79,7 +87,7 @@ describe("AvroSerializer", function () {
});

it("works with trivial example in README", async () => {
const serializer = await createTestSerializer();
const serializer = await createTestSerializer({ recorder });

// Example Avro schema
const schema = JSON.stringify({
Expand All @@ -102,7 +110,7 @@ describe("AvroSerializer", function () {
});

it("deserializes from a compatible reader schema", async () => {
const serializer = await createTestSerializer();
const serializer = await createTestSerializer({ recorder });
const message = await serializer.serialize(testValue, testSchema);
const deserializedValue: any = await serializer.deserialize(message, {
/**
Expand Down Expand Up @@ -132,7 +140,6 @@ describe("AvroSerializer", function () {
});

it("ignores the old format", async () => {
const registry = createTestRegistry();
const schemaId = await registerTestSchema(registry);
const serializer = await createTestSerializer<MessageContent>({
...noAutoRegisterOptions,
Expand All @@ -157,7 +164,7 @@ describe("AvroSerializer", function () {
* This test is very expensive to run in live because it registers too many
* schemas but the standard-tier resource allows for up to 25 schemas only
*/
if (isLive) {
if (isLiveMode()) {
this.skip();
}
function makeRndStr(length: number): string {
Expand All @@ -169,8 +176,7 @@ describe("AvroSerializer", function () {
return result;
}

const registry = createTestRegistry();
const serializer = await createTestSerializer({ registry });
const serializer = await createTestSerializer({ registry, recorder });
/**
* The standard tier resource supports registering up to 25 schemas per a schema group.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import {
Subscription,
earliestEventPosition,
latestEventPosition,
EventHubConsumerClientOptions,
} from "@azure/event-hubs";
import { MessagingTestClient } from "./models";
import { delay } from "@azure-tools/test-recorder";
import { delay, Recorder } from "@azure-tools/test-recorder";

export function createEventHubsClient(settings: {
eventHubsConnectionString: string;
eventHubName: string;
alreadyEnqueued: boolean;
recorder?: Recorder;
}): MessagingTestClient<EventData> {
const { alreadyEnqueued, eventHubName, eventHubsConnectionString } = settings;
const { alreadyEnqueued, eventHubName, eventHubsConnectionString, recorder } = settings;
let producer: EventHubBufferedProducerClient;
let consumer: EventHubConsumerClient;
let subscription: Subscription;
Expand All @@ -30,16 +32,22 @@ export function createEventHubsClient(settings: {
return initialized;
},
async initialize() {
producer = new EventHubBufferedProducerClient(eventHubsConnectionString, eventHubName, {
const clientOptions = {
onSendEventsErrorHandler: (ctx: OnSendEventsErrorContext) => {
this.cleanup();
throw ctx.error;
},
});
};
producer = new EventHubBufferedProducerClient(
eventHubsConnectionString,
eventHubName,
recorder?.configureClientOptions(clientOptions) ?? clientOptions
);
consumer = new EventHubConsumerClient(
EventHubConsumerClient.defaultConsumerGroupName,
eventHubsConnectionString,
eventHubName
eventHubName,
recorder?.configureClientOptions<EventHubConsumerClientOptions>({}) ?? undefined
);
subscription = consumer.subscribe(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

import { MessagingTestClient } from "./models";
import { isLive } from "./../utils/isLive";
import { isLiveMode } from "@azure-tools/test-recorder";

/**
* Returns a mocked messaging client that can work in both live and playback modes.
Expand All @@ -12,7 +12,7 @@ import { isLive } from "./../utils/isLive";
export function createMockedMessagingClient<MessageT>(
createLiveClient: () => MessagingTestClient<MessageT>
): MessagingTestClient<MessageT> {
if (isLive) {
if (isLiveMode()) {
return createLiveClient();
}
const messageBuffer: MessageT[] = [];
Expand Down
18 changes: 13 additions & 5 deletions sdk/schemaregistry/schema-registry-avro/test/public/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,38 @@ import { assertError } from "./utils/assertError";
import chaiPromises from "chai-as-promised";
import { createTestRegistry } from "./utils/mockedRegistryClient";
import { createTestSerializer } from "./utils/mockedSerializer";
import { isLive } from "./utils/isLive";
import { testGroup } from "./utils/dummies";
import { v4 as uuid } from "uuid";
import { isLiveMode, Recorder } from "@azure-tools/test-recorder";

chaiUse(chaiPromises);

describe("Error scenarios", function () {
let serializer: AvroSerializer;
let registry: SchemaRegistry;
let serializerNoAutoReg: AvroSerializer;
before(async function () {
registry = createTestRegistry();
let recorder: Recorder;

beforeEach(async function () {
recorder = new Recorder(this.currentTest);
registry = createTestRegistry({ recorder });
serializer = await createTestSerializer({
registry,
serializerOptions: {
autoRegisterSchemas: true,
groupName: testGroup,
},
recorder,
});
serializerNoAutoReg = await createTestSerializer({
serializerOptions: {
autoRegisterSchemas: false,
groupName: testGroup,
},
recorder,
});
});

describe("Schema validation", function () {
it("unrecognized content type", async function () {
await assert.isRejected(
Expand Down Expand Up @@ -171,7 +177,7 @@ describe("Error scenarios", function () {
/**
* This test can not run in live mode because the service will validate the schema.
*/
if (isLive) {
if (isLiveMode()) {
this.skip();
}
const { id } = await registry.registerSchema({
Expand Down Expand Up @@ -410,7 +416,7 @@ describe("Error scenarios", function () {
* onResponse is not implemented in the mocked registry because it will
* add very little value so the test is skipped in playback mode.
*/
if (!isLive) {
if (!isLiveMode()) {
this.skip();
}
let ran = false;
Expand All @@ -420,13 +426,15 @@ describe("Error scenarios", function () {
ran = true;
},
},
recorder,
});
const customSerializer = await createTestSerializer({
registry: unusedRegistry,
serializerOptions: {
autoRegisterSchemas: true,
groupName: testGroup,
},
recorder,
});
await assertError(
customSerializer.serialize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

import * as avro from "avsc/";
import { env } from "./env";
import { env } from "@azure-tools/test-recorder";

export const testSchemaObject: avro.schema.RecordType = {
type: "record",
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 896e9ae

Please sign in to comment.