description: serverMonitoringMode

schemaVersion: "1.17"
# These tests cannot run on replica sets because the order of the expected
# SDAM events are non-deterministic when monitoring multiple servers.
# They also cannot run on Serverless or load balanced clusters where SDAM is disabled.
runOnRequirements:
  - topologies: [single, sharded, sharded-replicaset]
    serverless: forbid
tests:
  - description: "connect with serverMonitoringMode=auto >=4.4"
    runOnRequirements:
      - minServerVersion: "4.4.0"
    operations:
      - name: createEntities
        object: testRunner
        arguments:
          entities:
            - client:
                id: client
                uriOptions:
                  serverMonitoringMode: "auto"
                useMultipleMongoses: false
                observeEvents:
                  - serverHeartbeatStartedEvent
                  - serverHeartbeatSucceededEvent
                  - serverHeartbeatFailedEvent
            - database:
                id: db
                client: client
                databaseName: sdam-tests
      - &ping
        name: runCommand
        object: db
        arguments:
          commandName: ping
          command: { ping: 1 }
        expectResult: { ok: 1 }
      # Wait for the second serverHeartbeatStartedEvent to ensure we start streaming.
      - &waitForSecondHeartbeatStarted
        name: waitForEvent
        object: testRunner
        arguments:
          client: client
          event:
            serverHeartbeatStartedEvent: {}
          count: 2
    expectEvents: &streamingStartedEvents
      - client: client
        eventType: sdam
        ignoreExtraEvents: true
        events:
          - serverHeartbeatStartedEvent:
              awaited: False
          - serverHeartbeatSucceededEvent:
              awaited: False
          - serverHeartbeatStartedEvent:
              awaited: True

  - description: "connect with serverMonitoringMode=auto <4.4"
    runOnRequirements:
      - maxServerVersion: "4.2.99"
    operations:
      - name: createEntities
        object: testRunner
        arguments:
          entities:
            - client:
                id: client
                uriOptions:
                  serverMonitoringMode: "auto"
                  heartbeatFrequencyMS: 500
                useMultipleMongoses: false
                observeEvents:
                  - serverHeartbeatStartedEvent
                  - serverHeartbeatSucceededEvent
                  - serverHeartbeatFailedEvent
            - database:
                id: db
                client: client
                databaseName: sdam-tests
      - *ping
      # Wait for the second serverHeartbeatStartedEvent to ensure we do not stream.
      - *waitForSecondHeartbeatStarted
    expectEvents: &pollingStartedEvents
      - client: client
        eventType: sdam
        ignoreExtraEvents: true
        events:
          - serverHeartbeatStartedEvent:
              awaited: False
          - serverHeartbeatSucceededEvent:
              awaited: False
          - serverHeartbeatStartedEvent:
              awaited: False

  - description: "connect with serverMonitoringMode=stream >=4.4"
    runOnRequirements:
      - minServerVersion: "4.4.0"
    operations:
      - name: createEntities
        object: testRunner
        arguments:
          entities:
            - client:
                id: client
                uriOptions:
                  serverMonitoringMode: "stream"
                useMultipleMongoses: false
                observeEvents:
                  - serverHeartbeatStartedEvent
                  - serverHeartbeatSucceededEvent
                  - serverHeartbeatFailedEvent
            - database:
                id: db
                client: client
                databaseName: sdam-tests
      - *ping
      # Wait for the second serverHeartbeatStartedEvent to ensure we start streaming.
      - *waitForSecondHeartbeatStarted
    expectEvents: *streamingStartedEvents

  - description: "connect with serverMonitoringMode=stream <4.4"
    runOnRequirements:
      - maxServerVersion: "4.2.99"
    operations:
      - name: createEntities
        object: testRunner
        arguments:
          entities:
            - client:
                id: client
                uriOptions:
                  serverMonitoringMode: "stream"
                  heartbeatFrequencyMS: 500
                useMultipleMongoses: false
                observeEvents:
                  - serverHeartbeatStartedEvent
                  - serverHeartbeatSucceededEvent
                  - serverHeartbeatFailedEvent
            - database:
                id: db
                client: client
                databaseName: sdam-tests
      - *ping
      # Wait for the second serverHeartbeatStartedEvent to ensure we do not stream.
      - *waitForSecondHeartbeatStarted
    expectEvents: *pollingStartedEvents

  - description: "connect with serverMonitoringMode=poll"
    operations:
      - name: createEntities
        object: testRunner
        arguments:
          entities:
            - client:
                id: client
                uriOptions:
                  serverMonitoringMode: "poll"
                  heartbeatFrequencyMS: 500
                useMultipleMongoses: false
                observeEvents:
                  - serverHeartbeatStartedEvent
                  - serverHeartbeatSucceededEvent
                  - serverHeartbeatFailedEvent
            - database:
                id: db
                client: client
                databaseName: sdam-tests
      - *ping
      # Wait for the second serverHeartbeatStartedEvent to ensure we do not stream.
      - *waitForSecondHeartbeatStarted
    expectEvents: *pollingStartedEvents
