Skip to content

Bump minimum Swift to 5.9, adopt StrictConcurrency=complete, add GitHub Actions CI #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
changelog:
categories:
- title: SemVer Major
labels:
- ⚠️ semver/major
- title: SemVer Minor
labels:
- semver/minor
- title: SemVer Patch
labels:
- semver/patch
- title: Other Changes
labels:
- semver/none
18 changes: 18 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Main

on:
push:
branches: [main]
schedule:
- cron: "0 8,20 * * *"

jobs:
unit-tests:
name: Unit tests
uses: apple/swift-nio/.github/workflows/unit_tests.yml@main
with:
linux_5_9_arguments_override: "--explicit-target-dependency-import-check error"
linux_5_10_arguments_override: --explicit-target-dependency-import-check error"
linux_6_0_arguments_override: "-Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
58 changes: 58 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: PR

on:
pull_request:
types: [opened, reopened, synchronize]

jobs:
soundness:
name: Soundness
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
with:
license_header_check_project_name: "swift-etcd-client-gsoc"

unit-tests:
name: Unit tests
uses: apple/swift-nio/.github/workflows/unit_tests.yml@main
with:
linux_5_9_arguments_override: "--explicit-target-dependency-import-check error"
linux_5_10_arguments_override: "--explicit-target-dependency-import-check error"
linux_6_0_arguments_override: "-Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"

# Integration tests use a running etcd so this job uses a service container.
integration-tests:
name: Integration tests
runs-on: ubuntu-latest
services:
etcd:
image: quay.io/coreos/etcd:v3.5.6
env:
ETCD_ADVERTISE_CLIENT_URLS: http://0.0.0.0:2379
ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379
ETCD_INITIAL_CLUSTER_STATE: new
ports:
- 2379:2379
container:
image: swift:6.0-noble
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Build package
run: swift build --build-tests
- name: Run integration tests
shell: bash # explicitly choose bash, which ensures -o pipefail
run: swift test --filter "IntegrationTests" | tee test.out
env:
SWIFT_ETCD_CLIENT_INTEGRATION_TEST_ENABLED: true
ETCD_HOST: etcd
ETCD_PORT: 2379
- name: Check integration tests actually did run
run: test -r test.out && ! grep -i -e "executed 0 tests" -e "skipped" test.out

cxx-interop:
name: Cxx interop
uses: apple/swift-nio/.github/workflows/cxx_interop.yml@main
18 changes: 18 additions & 0 deletions .github/workflows/pull_request_label.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: PR label

on:
pull_request:
types: [labeled, unlabeled, opened, reopened, synchronize]

jobs:
semver-label-check:
name: Semantic version label check
runs-on: ubuntu-latest
timeout-minutes: 1
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Check for Semantic Version label
uses: apple/swift-nio/.github/actions/pull_request_semver_label_checker@main
40 changes: 40 additions & 0 deletions .licenseignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.gitignore
**/.gitignore
.licenseignore
.gitattributes
.git-blame-ignore-revs
.mailfilter
.mailmap
.spi.yml
.swiftformat
.swift-format
.swiftformatignore
.editorconfig
.github/*
*.md
*.txt
*.yml
*.yaml
*.json
*.proto
*.pb.swift
*.grpc.swift
Package.swift
**/Package.swift
Package@-*.swift
**/Package@-*.swift
Package.resolved
**/Package.resolved
Makefile
*.modulemap
**/*.modulemap
**/*.docc/*
*.xcprivacy
**/*.xcprivacy
*.symlink
**/*.symlink
Dockerfile
**/Dockerfile
Snippets/*
dev/git.commit.template
.unacceptablelanguageignore
10 changes: 8 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.7
// swift-tools-version: 5.9
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-etcd-client-gsoc open source project
Expand Down Expand Up @@ -27,7 +27,7 @@ let package = Package(
.library(
name: "ETCD",
targets: ["ETCD"]
),
)
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.56.0"),
Expand Down Expand Up @@ -59,3 +59,9 @@ let package = Package(
),
]
)

for target in package.targets {
var settings = target.swiftSettings ?? []
settings.append(.enableExperimentalFeature("StrictConcurrency=complete"))
target.swiftSettings = settings
}
6 changes: 3 additions & 3 deletions Sources/ETCD/DeleteRangeRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public struct DeleteRangeRequest {
public var key: Data
public var rangeEnd: Data?
public var prevKV: Bool = false

init(protoDeleteRangeRequest: Etcdserverpb_DeleteRangeRequest) {
self.key = protoDeleteRangeRequest.key
self.rangeEnd = protoDeleteRangeRequest.rangeEnd.isEmpty ? nil : protoDeleteRangeRequest.rangeEnd
self.prevKV = protoDeleteRangeRequest.prevKv
}

/// Struct representing a deleteRangeRequest in etcd.
///
/// - Parameters:
Expand All @@ -37,7 +37,7 @@ public struct DeleteRangeRequest {
self.rangeEnd = rangeEnd
self.prevKV = prevKV
}

func toProto() -> Etcdserverpb_DeleteRangeRequest {
var protoDeleteRangeRequest = Etcdserverpb_DeleteRangeRequest()
protoDeleteRangeRequest.key = self.key
Expand Down
10 changes: 8 additions & 2 deletions Sources/ETCD/ETCDClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ public final class EtcdClient: @unchecked Sendable {
/// - Parameters:
/// - key: The key for which the value is watched. Parameter is of type Sequence<UInt8>.
/// - operation: The operation to be run on the WatchAsyncSequence for the key.
public func watch<Result>(_ key: some Sequence<UInt8>, _ operation: (WatchAsyncSequence) async throws -> Result) async throws -> Result {
public func watch<Result>(
_ key: some Sequence<UInt8>,
_ operation: (WatchAsyncSequence) async throws -> Result
) async throws -> Result {
let request = [Etcdserverpb_WatchRequest.with { $0.createRequest.key = Data(key) }]
let watchAsyncSequence = WatchAsyncSequence(grpcAsyncSequence: watchClient.watch(request))
return try await operation(watchAsyncSequence)
Expand All @@ -121,7 +124,10 @@ public final class EtcdClient: @unchecked Sendable {
/// - Parameters:
/// - key: The key for which the value is watched. Parameter is of type String.
/// - operation: The operation to be run on the WatchAsyncSequence for the key.
public func watch<Result>(_ key: String, _ operation: (WatchAsyncSequence) async throws -> Result) async throws -> Result {
public func watch<Result>(
_ key: String,
_ operation: (WatchAsyncSequence) async throws -> Result
) async throws -> Result {
try await watch(key.utf8, operation)
}
}
4 changes: 2 additions & 2 deletions Sources/ETCDExample/ETCDExample.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Dispatch

//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-etcd-client-gsoc open source project
Expand All @@ -13,6 +11,8 @@ import Dispatch
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Dispatch
import ETCD
import NIO

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import ETCD
import NIO

//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-etcd-client-gsoc open source project
Expand All @@ -14,18 +11,22 @@ import NIO
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import ETCD
import NIO
import XCTest

final class EtcdClientTests: XCTestCase {
var eventLoopGroup: EventLoopGroup!
var etcdClient: EtcdClient!
final class IntegrationTests: XCTestCase {

private static let integrationTestEnabled = getBoolEnv("SWIFT_ETCD_CLIENT_INTEGRATION_TEST_ENABLED") ?? false

override func setUp() async throws {
eventLoopGroup = MultiThreadedEventLoopGroup.singleton
etcdClient = EtcdClient(host: "localhost", port: 2379, eventLoopGroup: eventLoopGroup)
try XCTSkipUnless(Self.integrationTestEnabled)
}

func testSetAndGetStringValue() async throws {
let etcdClient = EtcdClient.testClient

try await etcdClient.set("testKey", value: "testValue")
let key = "testKey".data(using: .utf8)!
let rangeRequest = RangeRequest(key: key)
Expand All @@ -36,13 +37,17 @@ final class EtcdClientTests: XCTestCase {
}

func testGetNonExistentKey() async throws {
let etcdClient = EtcdClient.testClient

let key = "nonExistentKey".data(using: .utf8)!
let rangeRequest = RangeRequest(key: key)
let result = try await etcdClient.getRange(rangeRequest)
XCTAssertNil(result)
}

func testDeleteKeyExists() async throws {
let etcdClient = EtcdClient.testClient

let key = "testKey"
let value = "testValue"
try await etcdClient.set(key, value: value)
Expand All @@ -54,12 +59,14 @@ final class EtcdClientTests: XCTestCase {

let deleteRangeRequest = DeleteRangeRequest(key: rangeRequestKey)
try await etcdClient.deleteRange(deleteRangeRequest)

fetchedValue = try await etcdClient.getRange(rangeRequest)
XCTAssertNil(fetchedValue)
}

func testDeleteNonExistentKey() async throws {
let etcdClient = EtcdClient.testClient

let key = "testKey".data(using: .utf8)!
let rangeRequest = RangeRequest(key: key)

Expand All @@ -68,12 +75,14 @@ final class EtcdClientTests: XCTestCase {

let deleteRangeRequest = DeleteRangeRequest(key: key)
try await etcdClient.deleteRange(deleteRangeRequest)

fetchedValue = try await etcdClient.getRange(rangeRequest)
XCTAssertNil(fetchedValue)
}

func testUpdateExistingKey() async throws {
let etcdClient = EtcdClient.testClient

let key = "testKey"
let value = "testValue"
try await etcdClient.set(key, value: value)
Expand All @@ -95,14 +104,16 @@ final class EtcdClientTests: XCTestCase {
}

func testWatch() async throws {
let etcdClient = EtcdClient.testClient

let key = "testKey"
let value = "testValue".data(using: .utf8)!

try await etcdClient.put(key, value: "foo")

try await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
try await self.etcdClient.watch(key) { watchAsyncSequence in
try await etcdClient.watch(key) { watchAsyncSequence in
var iterator = watchAsyncSequence.makeAsyncIterator()
let events = try await iterator.next()
guard let events = events else {
Expand All @@ -122,8 +133,27 @@ final class EtcdClientTests: XCTestCase {
}

try await Task.sleep(nanoseconds: 1_000_000_000)
try await self.etcdClient.put(key, value: String(data: value, encoding: .utf8)!)
try await etcdClient.put(key, value: String(data: value, encoding: .utf8)!)
group.cancelAll()
}
}
}

extension EtcdClient {
fileprivate static let testClient = EtcdClient(
host: ProcessInfo.processInfo.environment["ETCD_HOST"] ?? "localhost",
port: getIntEnv("ETCD_PORT") ?? 2379,
eventLoopGroup: .singletonMultiThreadedEventLoopGroup
)
}

/// Returns true if `key` is a truthy string, otherwise returns false.
private func getBoolEnv(_ key: String) -> Bool? {
switch ProcessInfo.processInfo.environment[key]?.lowercased() {
case .none: return nil
case "true", "y", "yes", "on", "1": return true
default: return false
}
}

private func getIntEnv(_ key: String) -> Int? { ProcessInfo.processInfo.environment[key].flatMap(Int.init(_:)) }
Loading
Loading