description: "server-selection-logging"

schemaVersion: "1.14"

runOnRequirements:
  - topologies:
    - sharded

createEntities:
  - client:
      id: &client client
      uriOptions:
        retryWrites: false # so we don't retry after hitting a failpoint
        heartbeatFrequencyMS: 500
        appName: &appName loggingClient
        serverSelectionTimeoutMS: 2000 # avoid hanging for 30s in test expecting SS failure due to timeout
      observeLogMessages:
        serverSelection: debug
      observeEvents:
        - serverDescriptionChangedEvent
        - topologyDescriptionChangedEvent
      useMultipleMongoses: false
  - database:
      id: &database database
      client: *client
      databaseName: &databaseName logging-tests
  - collection:
      id: &collection collection
      database: *database
      collectionName: &collectionName server-selection
  - client:
      id: &failPointClient failPointClient
      useMultipleMongoses: false

tests:
  - description: "A successful operation"
    operations:
      # ensure we've discovered the entire topology before starting.
      # expected events are initial cluster type change from unknown to sharded, and connect events for one mongos.
      - name: waitForEvent
        object: testRunner
        arguments:
          client: *client
          event:
            topologyDescriptionChangedEvent: {}
          count: 2
      - name: insertOne
        object: *collection
        arguments:
          document: { x : 1 }
    expectLogMessages:
      - client: *client
        messages:
          - level: debug
            component: serverSelection
            data:
              message: "Server selection started"
              selector: { $$exists: true }
              operation: insert
              topologyDescription: { $$exists: true }
          - level: debug
            component: serverSelection
            data:
              message: "Server selection succeeded"
              selector: { $$exists: true }
              operation: insert
              topologyDescription: { $$exists: true }
              serverHost: { $$type: string }
              serverPort: { $$type: [int, long] }

  - description: "Failure due to unreachable server"
    runOnRequirements:
      - minServerVersion: "4.4" # failCommand appname support
    operations:
      # fail all hello/legacy hello commands for the main client.
      - name: failPoint
        object: testRunner
        arguments:
          client: *failPointClient
          failPoint:
            configureFailPoint: failCommand
            mode: alwaysOn
            data:
              failCommands: ["hello", "ismaster"]
              appName: *appName
              closeConnection: true
      # wait until we've marked the server unknown due
      # to a failed heartbeat.
      - name: waitForEvent
        object: testRunner
        arguments:
          client: *client
          event:
            serverDescriptionChangedEvent:
              newDescription:
                type: Unknown
          count: 1
      - name: insertOne
        object: *collection
        arguments:
          document: { x : 1 }
        expectError:
          isClientError: true # server selection timeout
    expectLogMessages:
      - client: *client
        messages:
          - level: debug
            component: serverSelection
            data:
              message: "Server selection started"
              selector: { $$exists: true }
              operation: insert
              topologyDescription: { $$exists: true }
          - level: info
            component: serverSelection
            data:
              message: "Waiting for suitable server to become available"
              selector: { $$exists: true }
              operation: insert
              topologyDescription: { $$exists: true }
              remainingTimeMS: { $$type: [int, long] }
          - level: debug
            component: serverSelection
            data:
              message: "Server selection failed"
              selector: { $$exists: true }
              operation: insert
              topologyDescription: { $$exists: true }
              failure: { $$exists: true }
