diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml
new file mode 100644
index 0000000..e2201d1
--- /dev/null
+++ b/.github/workflows/test.yaml
@@ -0,0 +1,62 @@
+on:
+ push:
+ branches:
+ - master
+ pull_request:
+ branches:
+ - master
+
+name: run tests
+jobs:
+ lint:
+ runs-on: ubuntu-latest
+ steps:
+ - name: Install Go
+ uses: actions/setup-go@v2
+ with:
+ go-version: 1.15.x
+ - name: Checkout code
+ uses: actions/checkout@v2
+ - name: Run linters
+ uses: golangci/golangci-lint-action@v2
+ with:
+ version: v1.29
+
+ test:
+ strategy:
+ matrix:
+ go-version: [1.15.x]
+ platform: [ubuntu-latest, macos-latest, windows-latest]
+ runs-on: ${{ matrix.platform }}
+ steps:
+ - name: Install Go
+ if: success()
+ uses: actions/setup-go@v2
+ with:
+ go-version: ${{ matrix.go-version }}
+ - name: Checkout code
+ uses: actions/checkout@v2
+ - name: Run tests
+ run: go test -v -covermode=count
+
+ coverage:
+ runs-on: ubuntu-latest
+ steps:
+ - name: Install Go
+ if: success()
+ uses: actions/setup-go@v2
+ with:
+ go-version: 1.15.x
+ - name: Checkout code
+ uses: actions/checkout@v2
+ - name: Calc coverage
+ run: |
+ go test -v -covermode=count -coverprofile=coverage.out
+ - name: Convert coverage.out to coverage.lcov
+ uses: jandelgado/gcov2lcov-action@v1.0.6
+ - name: Coveralls
+ uses: coverallsapp/github-action@v1.1.2
+ with:
+ github-token: ${{ secrets.github_token }}
+ path-to-lcov: coverage.lcov
+
diff --git a/readme.md b/readme.md
index 6668d25..5329349 100644
--- a/readme.md
+++ b/readme.md
@@ -3,26 +3,17 @@
An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).
- + ## Description @@ -35,7 +26,7 @@ Snowflake is a network service for generating unique ID numbers at high scale wi * The first bit is unused sign bit. * The second part consists of a 41-bit timestamp (milliseconds) whose value is the offset of the current time relative to a certain time. -* The 10 bits machineID, and max value is 2^10 -1 = 1023. +* The 10 bits machineID(5 bit workid + 5 bit datacenter id), max value is 2^10 -1 = 1023. * The last part consists of 12 bits, its means the length of the serial number generated per millisecond per working node, a maximum of 2^12 -1 = 4095 IDs can be generated in the same millisecond. * The binary length of 41 bits is at most 2^41 -1 millisecond = 69 years. So the snowflake algorithm can be used for up to 69 years, In order to maximize the use of the algorithm, you should specify a start time for it. @@ -180,8 +171,6 @@ snowflake.SetSequenceResolver(yourSequenceNumber) snowflake.ID() ``` -And you can use closure: - ## License MIT diff --git a/snowflake_test.go b/snowflake_test.go index ebb9e8b..8f2327c 100644 --- a/snowflake_test.go +++ b/snowflake_test.go @@ -194,9 +194,16 @@ func TestParseID(t *testing.T) { } func TestSID_GenerateTime(t *testing.T) { - sid := snowflake.ParseID(snowflake.ID()) + snowflake.SetSequenceResolver(snowflake.AtomicResolver) + a, e := snowflake.NextID() + if e != nil { + t.Error(e) + return + } + + sid := snowflake.ParseID(a) if sid.GenerateTime().UTC().Second() != time.Now().UTC().Second() { - t.Error("The id generate time should be equal") + t.Error("The id generate time should be equal current time") } }